top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to make a python script a service

+3 votes
506 views

I have a python script that is currently run from cron. We want to make it into a service so it can be controlled with service start/stop/restart.
Can anyone point me at site that has instructions on how to do this?

posted Dec 9, 2013 by Deepankar Dubey

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
The keyword your looking for here is "daemonize".
I don't just want to make it into a daemon - I want to control it with the service command.

1 Answer

+2 votes

Try to use this
http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/

it allows you start/stop/restart the script using the following commands.

python myscript.py start
python myscript.py stop
python myscript.py restart
answer Dec 9, 2013 by Sumit Pokharna
Turn it into a daemon as described, then take a look at the existing scripts in /etc/init.d/. There might even be a template in there iirc. Your script will likely be a simple wrapper around your daemonized python program.

After that, just do a "chkconfig --add “ where myscript is the name of your script in /etc/init.d.
Similar Questions
0 votes

I am running a python script and it will create a file name like filename0.0.0 and If I run it again then new file will create one more like filename0.0.1......
my code is-

i = 0
for i in range(1000):
 try:
 with open('filename%d.%d.%d.json'%(0,0,i,)): pass
 continue
 except IOError:
 dataFile = file('filename%d.%d.%d.json'%(0,0,i,), 'a+')
 break

But It will take more time after creating many files, So i want to store value of last var "i" in a variable so that when I run my script again then I can use it. for example-
my last created file is filename0.0.27 then it should store 27 in a variable and when i run again then new file should be created 0.0.28 according to last value "27", so that i could save time and it can create file fast..

0 votes

How to run a python script twice randomly in a day? Actually I want to run my script randomly in a day and twice only. Please help me.. how is it possible.

+2 votes

I recently installed Opennssh-server & mysql-server on my computer for development, but I require these services few time, I know I can start these service by using

sudo service start/stop/restart.

But how to disable them from starting on startup, becoz these tools made my computer bootup very slow.

...