简体   繁体   中英

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)

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

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.

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 ie

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM