3

So I want my server to run a script which starts several tmux sessions on boot.
This is my crontab:

@reboot . /home/steven/.profile; /home/steven/Scripts/Start.sh  

And this is the content of my Start.sh

#!/bin/bash  

cd ~/Scripts/  

echo "Mounting all drives..."
sudo mount -a
echo "Mounting finished."

echo "Starting Wemo Server..."
tmux new -s "wemo" -d
tmux send -t "wemo" $'./start_wemo_server.sh\n'
if (tmux list-sessions | grep "wemo");
    then
        echo "Wemo Server Ouimeaux started."
    else 
        echo "Tmux error @ wemo server."
fi

echo "Starting GoDaddy Dynamic DNS Service..."
tmux new -ds "godaddy"
tmux send -t "godaddy" $'./start_godaddy_dyndns.sh\n'
if (tmux list-sessions | grep "godaddy");
    then
        echo "GoDaddy Dynamic DNS Service started."
    else
        echo "Tmux error @ GoDaddy."
fi

echo "Starting Plex..."
sudo service plexmediaserver start
echo "Plex started."

echo "Starting PlexConnect Server..."
tmux new -ds "plex"
tmux send -t "plex" $'./start_plex_connect.sh\n'

if (tmux list-sessions | grep "plex");
    then
        echo "PlexConnect Server started."
    else
        echo "Tmux error @ Plex."
fi

echo "All starting sequences are finished. Quitting..."  

If I start this script manually (i.e. ./Scripts/Start.sh), everything works just fine, tmux loads my profile nicely and everything is okay. However if I set the crontab to start this script, tmux doesn't load the profile, .bashrc isn't loaded, I can't even use Up Arrow key to recall previous commands, or Tab to autocomplete.

This is really rather annoying because I use tmux during the day all the time, I can't live with a broken tmux session.

Does anyone have an idea of how this can be fixed or what I've done wrong?

Thank you.

7
  • 1
    Differences in behavior between cron execution and manual execution are usually due to differences in the environment variables during run. Make sure you're sourcing all your own startup files (in particular, /etc/profile, which often sources /etc/bashrc or similar on its own). You may also have to manually set $HOME (this is an important one).
    – Tom Hunt
    Commented Nov 25, 2015 at 0:03
  • If you don't source your own dotfiles (.bashrc, .profile, .bash_profile, etc.), then at least set the $PATH environment variable, which could be drastically different than what is getting loaded in your normal shell. This is just as important (if not more-so) than setting $HOME. Additionally, when running scripts via cronjob, it never hurts to include absolute paths. Most of the time, cronjobs do not start in the same directory as the script that is being executed. Commented Nov 25, 2015 at 2:19
  • The script is only starting services, why do that via crontab and not via your systems service manager?
    – Wieland
    Commented Nov 25, 2015 at 8:29
  • Read your .profile inside of the script instead of at the cron level.
    – chicks
    Commented Nov 25, 2015 at 13:29
  • @rubynorails how do you go about doing that? I'm a bit of a noob at Linux. I thought the . /home/steven/.profile would be the same as sourcing it?
    – Schteeb
    Commented Nov 26, 2015 at 8:52

1 Answer 1

1

Wherever your personal settings are loaded, depending on OS and preference, you need to source that file in the script so that it functions as it would if you were running it yourself in the terminal.

source or . is a command that functions sort of like import in other scripting/programming languages. Also note that if you use . instead of source, there should be a space between the . and the path to the profile you are sourcing:

. /home/username/.profile

At the beginning of your script, after #!/bin/bash, simply add source /home/yourusername/.profile (or .bashrc or whatever you are using to load your settings).

Double-check the permissions/ownership of the file, and make sure it's running in your personal crontab and not root's crontab, which would mess everything up if you were trying to run it as a normal user.

Also, like I said in my comment, it never hurts to include absolute paths in scripts that are being executed via cron. I believe you have multiple instances referencing ~/something or ./somescript. For instance, instead of cd ~/Scripts, use cd /home/username/Scripts, so that there can be no mistake of where this directory is located.

This is the point of source-ing your .profile, because that is normally where the $HOME variable (~) gets set. The way to avoid any discrepancy whatsoever, is just include absolutely paths to directories and scripts when you know jobs will be executed via cron.

1
  • So I've done this: #!/bin/bash source ./home/steven/.profile echo "Mounting all drives..." sudo mount -a echo "Mounting finished." echo "Starting Wemo Server..." tmux new -s "wemo" -d ... but the tmux sessions still aren't initialised with my .profile loaded
    – Schteeb
    Commented Nov 27, 2015 at 10:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .