How to Run a Service in the Background on Linux and Check if It’s Running
In this article, I will show you how to run a service in the background. I’ve used Apache Airflow as an example, but you can apply these methods to other services as well.
Running a Service in the Background
- Using
nohup
(Linux/MacOS):- The
nohup
command allows you to run the Airflow scheduler in the background and ensures it continues running even after you close the terminal.
nohup airflow scheduler > scheduler.log 2>&1 &
- Here,
nohup
keeps the process running,> scheduler.log 2>&1
saves the output to a log file, and&
runs it in the background.
- The
- Using
screen
(Linux/MacOS):screen
is a terminal multiplexer that lets you start a terminal session that you can detach from and reattach to later.
screen -S airflow-scheduler airflow scheduler
- After starting the scheduler, detach the screen session by pressing
Ctrl + A
, thenD
. To reattach to the session, use:
screen -r airflow-scheduler
- Using
tmux
(Linux/MacOS):tmux
is similar toscreen
, providing another way to manage terminal sessions that you can detach and reattach to.
tmux new -s airflow-scheduler airflow scheduler
- Detach from the session with
Ctrl + B
, thenD
. Reattach with:
tmux attach -t airflow-scheduler
- Using
systemd
(Linux):- To manage the Airflow scheduler as a service, you can create a
systemd
service file. This method is more permanent and reliable.
Create a file at
/etc/systemd/system/airflow-scheduler.service
with the following content:[Unit] Description=Airflow Scheduler After=network.target [Service] ExecStart=/usr/local/bin/airflow scheduler Restart=always User=airflow Group=airflow StandardOutput=syslog StandardError=syslog SyslogIdentifier=airflow-scheduler [Install] WantedBy=multi-user.target
- Then enable and start the service:
sudo systemctl enable airflow-scheduler sudo systemctl start airflow-scheduler
- To manage the Airflow scheduler as a service, you can create a
- Using
&
(Linux/MacOS):- For a quick and simple way to run the Airflow scheduler in the background, just append
&
to the command:
airflow scheduler &
- For a quick and simple way to run the Airflow scheduler in the background, just append
- Using Docker (If Airflow is running inside a container):
- If you’re using Docker, you can run the Airflow scheduler in detached mode using the following command:
docker run -d --name airflow-scheduler apache/airflow:latest scheduler
- This command will start the scheduler in the background inside the Docker container.
Checking if a Service is Running
- Checking the Airflow Scheduler:
- To see if the Airflow scheduler is running, use the
ps
command:
ps aux | grep 'airflow scheduler'
- This command will list the running processes, including the Airflow scheduler if it’s active.
- To see if the Airflow scheduler is running, use the
- Checking Apache Web Server:
- For Apache, which is a widely used web server, you can check its status with:
sudo systemctl status apache2
- If Apache is running, you’ll see “active (running)” in the status output.
Conclusion
So, the popular commands to run services in the background in linux like os are:nohup
, screen
, tmux
and systemctl