Linux Ubuntu 14.04 Have a script in ~/app/serve.py
Need to run it in this folder. In shell I may do
> cd ~/app && python serve.pyand it runs a server
Need to run it on servers boot in background. Tried to add this command cd ~/app && python serve.py ~ in etc/rc.local (and etc/init.d/rc.local) but it doesn't start.
What am I doing wrong?
2 Answers
Instead of adding cd ~/app && python serve.py in /etc/rc.local, try putting in the entire path. You are expecting the init process to know that ~ is your home directory somehow, but init starts as root. Because init starts as root, it's looking in / for the app directory not your home directory. Get rid of the ~ by using the path to your home directory.
Try adding /home/whitecolor/app/serve.py to /etc/rc.local. (pwd to find your pathway directory.) Get rid of the python in front of your serve.py script by adding #!/usr/bin/python at the top of your script. (type a which python to find the path in case it isn't installed in the normal place)
Edit: You did say "run in the background", and I missed that. To run a command in the background, add a & after the command. So, to background that command above: /home/whitecolor/app/serve.py & Init doesn't usually need the background symbol to start a job and run it in the background.
DrDR's excellent suggestion would not need the same & treatment after the command in the cron startup as those are run in the background by default.
I've never had the need to try this, but apparently scripts can be run at system startup via cron by adding a line similar to the one beginning with @reboot below.
#m h dom mon dow command
1 * * * * someHourlyCommand
@reboot python /home/<username>/app/serve.pyNote, I've changed the command slightly to eliminate cd'ing into the directory. Your situation may be different, so do what works for you.
2