Search code examples
bashcrontmuxmacos-catalina

How do I run a cron job that spawns a tmux session? (MacOS)


I'm on MacOS Catalina. I'm trying to run a cron job that spawns a named tmux session with windows. Here is the crontab -l:

* * * * * cd /Users/dev/project; ./start.sh; ./poll 2>> /tmp/cron.out

However I don't see my session with tmux ls. In my error logs cat /tmp/cron.out

./poll: line 3: tmux: command not found
./poll: line 5: tmux: command not found

This is the script I'm running. I have tmux installed for my user, and it works normally. When I execute poll normally, it works just fine.

Here is start.sh:

#!/bin/bash

tmux kill-session -t collect

tmux new -s "collect" -d ./stuff

Solution

  • If you run tmux in your regular terminal, it will search the $PATH variable to find the correct folder.

    Scripts that get executed by cron does not share the same environment $PATH variable as your user, therefore the script can't find the exatuable.

    You could add the $PATH to you script, like so:

    #!/bin/bash
    
    PATH=/usr/local/bin
    
    tmux kill-session -t collect
    
    tmux new -s "collect" -d ./stuff
    

    But I guess using the full path is in your case much more readable!

    Read more about $PATH on unix.stackexchange