Perhaps I'm using the incorrect vocabulary, but I can't seem to find a duplicate of my issue. I am trying to execute two programs within my shell script and cron is clearly executing the first script, but not the second.


I have two python scripts, views.py that runs as a web server and housekeeping.py that takes care of some cleanup and update checks that the web interface need not worry about. Currently, these can be manually started using sudo /home/ubuntu/start_server.sh and all works perfectly. I am using port 80, so I need to run as root.

Bash Script

The full bash script:


/home/ubuntu/py3env/bin/python /home/ubuntu/flask_portal/views.py &
/home/ubuntu/py3env/bin/python /home/ubuntu/flask_portal/housekeeping.py &

My understanding is that the & will cause applications to run in the background.

When I execute manually, the shell script works perfectly.


To open crontab, I am using

sudo crontab -u root -e

As I said above, I am using port 80 so I need to run my script as root.

Within the crontab file, I have a single entry:

@reboot /home/ubuntu/start_server.sh


When I execute by hand, both scripts start perfectly. When I allow the server to restart and run the start_server.sh script itself, the web server views.py works just fine, but housekeeping.py never starts (I'm doing logging within that file, so it isn't generating the proper files). In addition, when I execute python aux | grep python, I see that both views.py and housekeeping.py are executing.

  • If you're in a venv, don't you need to activate it? In all probability, the script is relying on some aspect of the environment that's available in a terminal and not when run by cron. – muru Dec 1 '16 at 3:15
  • I haven't needed to activate a virtual environment before, so long as I specify the path of the venv as the program to execute. – slightlynybbled Dec 1 '16 at 3:16
  • In that case, try sudo env -i /home/ubuntu/py3env/bin/python /home/ubuntu/flask_portal/housekeeping.py – muru Dec 1 '16 at 3:19
  • @muru I see that you wrote sudo in that line, but I can verify that the command is executing properly when I type in sudo ./start_server.sh. Is there something there that I should put in the crontab or in the shell script? – slightlynybbled Dec 1 '16 at 3:21
  • Looks like it is running the housekeeping.py file with no issues. – slightlynybbled Dec 1 '16 at 3:25

Thanks to muru, I was able to find the answer to my question.

As it happens, the bash script and the cron job are working correctly. The housekeeping.py had an error that occurs when executed as 'root' and not 'ubuntu', but which I could not see.

The error was revealed by adding the following to the top of the bash script:

exec 2>/home/ubuntu/log

Which has the effect of piping the output of all programs started in the bash script into a log file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.