Codementor Events

How to run a script as a background process?

Published Aug 17, 2017
How to run a script as a background process?

We all agree that at times we build stuff as a side project to test out things and eventually scrap them before bringing it into picture in a larger project. More so, we want to minimize the resource consumption on this side adventure and yet achieve the maximum closeness to the environment it is supposed to be used in.

One such scenario could be a script which is supposed to run as a background process on your local system or server. The script might be intended to be a daemon or a web service. While you might have proper environment set up to handle the dependencies for this script in production, nobody wants to go through setting up and configuring an environment to test out one single script in isolation.

Ever wondered how can we make that script that you just created run as a background process? Your reasons for making such a script may vary but if you are looking for a simple way to get out of this situation, this is for you.

To achieve this in a Debiansystem we are going to use Systemd(Check whether your OS is based on Debian). Systemd is the default system and service manager post Debian Jessie and should be available out of the box (reference). If not, you can go through the installation steps.

Alternatively to check whether your default init is systemd, you can use this handy script here

# sudo ps -p 1 -o comm=systemd

Lets dive in and create the background process. I’m assuming that the script that you wish to run in background is already handling the logic to iterate itself without exiting (if not the process will end with your script). Also, this example is built using python so for other languages you’ll have to fill in the missing pieces yourself.

The first thing you want to know is where the systemd processes reside. You can find them by going to :

# cd /etc/systemd/system/

Lets create our own service file (I have named it bg_process) and add some code to it:

# sudo touch bg_process.service
# sudo vim bg_process.service


[Unit]
Description=bg_process
After=syslog.target network.target

[Service]
Type=simple
User=root
ExecStart=/usr/bin/python /var/www/scripts/my_bg_script.py
Restart=on-abort

[Install]
WantedBy=multi-user.target

The only parameter required to be changed to make the above script functional for you is ExecStart . In general, your ExecStart will look something like this:

ExecStart=<path_to_python_installation> <path_to_script>

If you are using a virtual environment, simply replace the default python path with the path of your environment.

Most of the sections above are self explanatory but if you wish to read more about the parameters used and what other options are available to you, you can read this neat documentation from RedHat.

The only thing left to do is to let the system know that we have a new service to run and start it. Moving forward your script will be initialized on every system boot.

# sudo systemctl daemon-reload
# sudo service bg_process start

And voila your script is now a background service. To check for it’s status you can anytime run:

# sudo service bg_process status

A simple google search or browsing on stackoverfow can land you on a variety of solutions to achieve this. If this is something that is essential to your project, you might want to seriously consider all the options before settling on the correct solution.

Please feel free to add in your comments and suggestions.

Discover and read more posts from Saket Shukla
get started
post commentsBe the first to share your opinion
Show more replies