0

I am new to python and have pretty basic knowledge of Linux.

I need to start a script at boot time on a Ubuntu 14.04.3 server.

The only thing is, the script is a monitoring tool and should be running all the time so I can't just make a periodical cron call.

I found this at first : running a python script with cron

I have tried to add this in crontab :

@reboot python /path/to/script.py &

And also this :

@reboot /path/to/script.py &

but it doesn't seems to work.

I have also seen this : How to make a python script run like a service or daemon in linux

The main answer is cron or a change in the python code.

So my question is : Is there another way to run my script at boot and let it run "forever" without changing the code ?

I assure you if I don't want to change the code it's not by lazyness but I will if it's the only option.

Other information (don't know if it's necessary), I am running Windows and have access to the server via PuTTY. The version of Python is 2.7

UPDATE

Here is the cron log :

Nov 27 15:57:03 trustyovh cron[760]: (CRON) INFO (pidfile fd = 3)
Nov 27 15:57:03 trustyovh cron[798]: (CRON) STARTUP (fork ok)
Nov 27 15:57:03 trustyovh cron[798]: (CRON) INFO (Running @reboot jobs)
Nov 27 15:57:03 trustyovh CRON[807]: (administrateur) CMD (/home/administrateur/scuMonitor/main.py &)
Nov 27 15:57:03 trustyovh CRON[800]: (CRON) info (No MTA installed, discarding output)
Nov 27 16:09:01 trustyovh CRON[1792]: (root) CMD (  [ -x /usr/lib/php5/maxlifetime ] && [ -x /usr/lib/php5/sessionclean ] && [ -d /var/lib/php5 ] && /usr/lib/php5/sessionclean /var/lib/php5 $(/usr/lib/php5/maxlifetime))

Here is the crontab :

@reboot /home/administrateur/scuMonitor/main.py &

UPDATE 2

Well, it was actually working with the cron set to reboot, but, my script didn't put his log where I expected it to do (I wasn't understanding how the path work on Linux).

Thanks for all the answers everyone !

erjon
  • 149
  • 2
  • 3
  • 11
  • Elaborate on "but it doesn't seems to work." - have you checked if cron sends you any errors? Have checked cron logs? Plus, throw away ampersand character from cron entries. – Michał Fita Nov 27 '15 at 10:53
  • @MichałF No, I have not done that, where do I find the logs and errors please ? – erjon Nov 27 '15 at 10:56
  • It may be /var/log/syslog, or if your system is configured differently it may be file dedicated for cron. Try `sudo grep -R CRON /var/log | head` to check which log files contains entries from CRON. – Michał Fita Nov 27 '15 at 11:03
  • So my cron log is in `/var/log/cron.log` and my command do appear in it, but it doesn't run, my script add a line into his own log file every minute when it is running. But there is no new line. – erjon Nov 27 '15 at 14:55
  • If your command appears in the log, that means CRON processed that line and it has to be some outcome from that processing, an error or something? Could you post couple lines from CRON around the line where your script is present? Are you setting up cron as root or as a user? Do you use `crontab -e` command? – Michał Fita Nov 27 '15 at 15:05
  • I do use `crontab -e`, I set up cron as a user, I will update the post to show you the log. – erjon Nov 27 '15 at 15:15
  • Am I correctly understand `/home/administrateur/scuMonitor/main.py` is your script? Do you have proper permissions set on this script? Remove `&` character from crontab entry. It's not needed as cron doesn't schedule background jobs but just executes a command. In fact the presence of & may cause your script not to work, because CRON starts a shell, where it executes the command; that shell send your script to the background and ends, but then every process in the background ends as well. You would need to use `nohup` with `&`. – Michał Fita Nov 27 '15 at 15:24
  • I will try that, thanks for your time. – erjon Nov 27 '15 at 15:32

2 Answers2

7

I would suggest you the same thing I have written here

Basically, you can run your python code as a service using systemd, all you have to do is to write a <your-app-name>.service file, like the one below

[Unit]
Description=Some kind of description

[Service]
Type=simple
ExecStart=<path to your bin with args if needed>

then, save it under /etc/systemd/system/. To check if everything is fine, run

sudo systemctl start <your-app-name>

and then

sudo systemctl status <your-app-name>

Finally run

sudo systemctl enable <your-app-name>

and the service will be executed at each system boot.

Community
  • 1
  • 1
Carlo Lobrano
  • 368
  • 1
  • 9
3

Taken from Run Python script at startup in Ubuntu. You can start a service on Ubuntu by adding it to the /etc/init directory.

Put this in /etc/init

mystartupscript.conf

start on runlevel [2345]
stop on runlevel [!2345]

exec /path/to/script.py

As far as i know the only way to keep it checking for what ever you want it to check for is by implementing a loop in the code/daemonizing.

Community
  • 1
  • 1