Idan Shuster
Idan Shuster

Reputation: 63

How to run python script at startup

I'm trying to make a Python script run as a service.

It need to work and run automatically after a reboot. I have tried to copy it inside the init.d folder, But without any luck.

Can anyone help?(if it demands a cronjob, i haven't configured one before, so i would be glad if you could write how to do it)

(Running Centos)

Upvotes: 2

Views: 11270

Answers (3)

kamran kausar
kamran kausar

Reputation: 4593

For Ubuntu Variant:- open the /etc/rc.local file with:

nano /etc/rc.local

add the following line just before the exit 0 line:

start-stop-daemon -b -S -x python /root/python/test.py

or

Give absolute path of your command i.e

nohup /usr/bin/python2 /home/kamran/auto_run_py_script_1.py &

The start-stop-daemon command creates a daemon to handle the execution of our program. The -b switch causes the program to be executed in the background. The -S switch tells the daemon to start our program. And the -x switch tells the daemon that our program is an executable.

To check and Run

sudo sh /etc/rc.local

Upvotes: 0

ramsay
ramsay

Reputation: 3835

run this command

crontab -e

and then add

@reboot /usr/bin/python /path/to/yourpythonscript

save and quit,then your python script will automatically run after you reboot

Upvotes: 9

Max Murphy
Max Murphy

Reputation: 1973

There is no intrinsic reason why Python should be different from any other scripting language here.

Here is someone else using python in init.d: blog.scphillips.com/posts/2013/07/… In fact, that deals with a lot that I don't deal with here, so I recommend just following that post.

Upvotes: 0

Related Questions