简体   繁体   中英

how to run python script as a service in Linux

I have a python script that I would like to be used as a service in Suse Linux.

I tried creating a script.conf file in /etc/init.d that looks like this:

description "File service"
author  "Me"

start on runlevel [234]
stop on runlevel [0156]

chdir /app/file/bin
exec /app/file/bin/script.py
respawn

How do I test if this script is working ? I have no idea what command to type to start running this service. Any answers to this will be greatly appreciated thanks.

Lets break answer in two parts first how to configure it and Second how to run and test it as process.

How to configure?

Configuration you have posted above is correct. But file is supposed to be created under directory /etc/init/myscript-service.conf (That's what I follow in my scenario). Another configuration part is you have to make script file executable so that it can run as service.

Linux command

chmod u+x /app/file/bin/script.py

You need to add below line in your python script.

#!/usr/bin/python

And install upstart tool.

After above all configuration you are ready to run script as service.

How to run and test?

Once you have created file myscript-service.conf you can start the service as normal service in linux using below command.

service myscript-service start

It will give you process id. And you can start/stop/status it like normal service.

Logs you can find under

/var/log/upstart/myscript-service.log

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