linux process managemtn

Linux Process Management Tutorial For Developers

Linux is a multitasking system, which means it can run multiple tasks (process) at a time. Basically, a process is a running program or a task or a command on your Linux system.

Linux Process Management for Developers

When you are developing applications on Linux, you will be using a web server, database server etc..At times, you might want to have a look at the process or may want to terminate and restart it. In this article, we will cover some of the basic process related tips for developers working on Linux based systems.

Process Management Daemons

Every Linux system has one of the following process management daemons. It will be the first process to be started on the system when you boot it up.

1. Init – SysV (first adoption – historic)
2. Upstart – (first adoption – 2006)
3. Systemd (first adoption – 2011)

Note: In the latest OS releases, systemd is been widely used. in ubuntu 15.05, cenots 7, init is replaced by systemd.

Process related commands

You can list all the process in Linux using ps command.

ps aux

To get a filtered meaningful output, you can use grep filtering utility. For example, if you want to list all the processes run by the vagrant user, you can execute the following command.

ps aux | grep vagrant

To get the process id  of a specific service, you can use pgrep with the service name as shown below.

pgrep httpd

To get the list of process in a tree structure, you can use the pstree command.

Note: pstree is not installed on Linux systems by default. You can install pstree from relevant distro repo.

pstree

To get the process id with the tree structure, use the -p flag.

pstree -p

If your system is little slow and if you want to get a dynamic view of processes, you can use the top command. Top updates itself in real-time based on the CPU and memory usage by the processes.

top

To terminate a running process, you can use kill and killall command.

If you know the process id, you can use the kill command as shown below.

kill 9506

To forcefully kill the process, you need to add -9 flag to the kill command.

kill -9 9506

If you know the name of the process, you can use the killall command.

killall apache2

Running Processes in Background

When you execute a command on a terminal, you will need to watch it executing until it finishes the execution. For longer running commands you actually run the process in the background.

To put a process in the background, just append the & at the end of the command.

For example, tail -f /var/log/syslog gives a dynamic view of log files when it is added subsequently. To put this execution in the background, you need to execute the command as shown below.

tail -f /var/log/syslog &

You can view the background jobs using jobs command.

For example,

[vagrant@devbox ~]$ jobs
[1]+  Running                 sudo tail -f /var/log/yum.log &
[vagrant@devbox ~]$

In the above example [1] is the job id. To bring the job running at the background to foreground, use the fg command with the job id.

For example,

fg 1

You can send the process to the background using the bg command. First, you need to suspend the process using ctrl+z.

For example,

bg 1

If you going to run a really long running process, you can append nohup which stands for no hanging up. The advantage of using nohup over & is even if you close your terminal the process will run without interruption.

For example, let’s say you are untaring an archive which might take half an hour. In this case, you can use nohup and close the terminal. The job will run in the background without any problem.

tar czf archive.tar.gz /var/* nohup

Starting and Stopping Services

You can start, stop and restart services using the service command

service <service name> start
service <service name> stop
service <service name> restart

Other Interesting Blogs

Leave a Comment

0 Shares
Share via
Copy link
Powered by Social Snap