how can i create a service which run simple python file

ArneIT used Ask the Experts™
Hi everyone,

i have a python file and i want to create a service to run it. how can i do that basically? furthermore, when i stop this service, this python file shouldn't be running.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
If you mean a scheduled job then you can set a job in the crontab:

sudo crontab -e

cronjobs are listed as : * * * * * command

So yours should be something like * * * * * /usr/bin/python /path-to-file

Positions of * mean the following:

Minute (0-59)  Hour (0-23)  Day of Month (1-31)  Month (1-12 or Jan-Dec)  Day of Week (0-6 or Sun-Sat)  Command

But if you want to set it up as a real service with a port you can define a new service in /etc/services: vi /etc/services.  Define name and port number  tcp/udp there
Then define the service in xinetd.conf and restart your network :/etc/init.d/networking restart

If you want to stop the service comment out the line in /etc/services and restart network.
In Linux you need to fork if you want to start the service from command line. If you want to start it automatically you need to put an entry in the /etc/inittab file sample is as shown below.


in the above example S0 is just an ID, 2345 are the runlevels when you want your service to run, respawn means to restart your service if it fails for some reason, and last is the executable to run.



Thanks, its solved by your hints.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial