Categories
Planning

How to run and control background processes on linux

In this guide, we shall bring to light a simple yet important concept in process handling in a Linux system, that is how to completely detach a process from its controlling terminal.

When a process is associated with a terminal, two problems might occur:

  1. your controlling terminal is filled with so much output data and error/diagnostic messages.
  2. in the event that the terminal is closed, the process together with its child processes will be terminated.

To deal with these two issues, you need to totally detach a process from a controlling terminal. Before we actually move to solve the problem, let us briefly cover how to run processes in the background.

How to Start a Linux Process or Command in Background

If a process is already in execution, such as the tar command example below, simply press Ctrl+Z to stop it then enter the command bg to continue with its execution in the background as a job.

You can view all your background jobs by typing jobs . However, its stdin, stdout, stderr are still joined to the terminal.

Run Linux Command in Background

You can as well run a process directly from the background using the ampersand, & sign.

Start Linux Process in Background

Take a look at the example below, although the tar command was started as a background job, an error message was still sent to the terminal meaning the process is still connected to the controlling terminal.

Linux Process Running in Background Message

Keep Linux Processes Running After Exiting Terminal

We will use disown command, it is used after the a process has been launched and put in the background, it’s work is to remove a shell job from the shell’s active list jobs, therefore you will not use fg , bg commands on that particular job anymore.

In addition, when you close the controlling terminal, the job will not hang or send a SIGHUP to any child jobs.

Let’s take a look at the below example of using diswon bash built-in function.

Keep Linux Process Running After Closing Terminal

You can also use nohup command, which also enables a process to continue running in the background when a user exits a shell.

Put Linux Process in Background After Closing Shell

Detach a Linux Processes From Controlling Terminal

Therefore, to completely detach a process from a controlling terminal, use the command format below, this is more effective for graphical user interface (GUI) applications such as firefox:

In Linux, /dev/null is a special device file which writes-off (gets rid of) all data written to it, in the command above, input is read from, and output is sent to /dev/null.

As a concluding remark, provided a process is connected to a controlling terminal, as a user, you will see several output lines of the process data as well as error messages on your terminal. Again, when you close the a controlling terminal, your process and child processes will be terminated.

Importantly, for any questions or remarks on the subject, reach us by using the comment form below.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

If you have a long-running task, it’s not always wise to wait for it to finish. I mean why keep the terminal occupied for a particular command? In Linux, you can send a command or process to background so that the command would be running but the terminal will be free for you to run other commands.

In this tutorial, I’ll show you a couple of ways to send a process in background. I’ll also show you how to bring the background processes back to foreground.

Start a Linux process in background directly

If you know that the command or process is going to take a long time, it would be a better idea to start the command in background itself.

To run a Linux command in background, all you have to do is to add ampersand (&) at the end of the command, like this:

Let’s take a simple bash sleep command and send it to background.

When the command finishes in the background, you should see information about that on the terminal.

Send a running Linux process to background

If you already ran a program and then realized that you should have run it in background, don’t worry. You can send a running process to background as well.

What you have to do here is to use Ctrl+Z to suspend the running process and then use ‘bg‘ (short for background) to send the process in background. The suspended process will now run in background.

Let’s take the same example as before.

See all processes running in background

Now that you know how to send the processes in background, you might be interested in knowing which commands are running in the background.

For this purpose, you can enter this command in the terminal:

Let’s put some commands in the background first.

Now the jobs command will show you all the running jobs/processes/commands in the background like this:

Do you notice the numbers [1], [2] and [3] etc? These are the job ids. You would also notice the – and + sign on two of the commands. The + sign indicates the last job you have run or foregrounded. The – sign indicates the second last job that you ran or foregrounded.

Bring a Process to Foreground in Linux

Alright! So you learned to run commands in background in Linux. But what about bringing a process running in the background to foreground again?

To send the command to background, you used ‘bg’. To bring background process back, use the command ‘fg’.

Now if you simply use fg, it will bring the last process in the background job queue to foreground. In our previous example, running ‘fg’ will bring Vim editor back to the terminal.

If you want to bring a certain process to the foreground, you need to specify its job id. The job id is the number you see at the beginning of each line in the output of the ‘jobs’ command.

Where n is the job id as displayed in the output of the command jobs.

That’s it

This was a quick one but enough for you to learn a few things about running commands in background in Linux. I would advise learning nohup command as well. This command lets you run commands in background even after you log out of the session.

If you have questions or suggestions, please leave a comment below.

Running commands or processes in the background on a Linux system becomes a common task if you need to free up your terminal or disconnect from an SSH session. This is especially true of commands that run for a long time, either to listen for events or complete their lengthy task.

We have a few options on Linux for running commands in the background. In this guide, we’ll cover a few different methods and show you how to use them through examples.

In this tutorial you will learn:

  • How to put a running command into the background
  • How to start a command in the background
  • How to close terminal and keep background job running
  • How to use the screen command

How to run and control background processes on linux

Software Requirements and Linux Command Line Conventions

Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Default Linux utilities, screen
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Put a running command into the background

When you execute a command that wants to continue running for a long time, the problem is that your current terminal gets dedicated to the command. You can’t continue using the terminal, and you also can’t close it, because that will end the command that’s currently being run.

If you have a command running, and need to put it into the background to free up your terminal, you can press Ctrl + Z on your keyboard to stop the process. Here’s an example.

To see a list of jobs in the background, use the jobs command.

To bring a job back to the foreground, we can use the fg command.

If we had multiple stopped jobs, we would need to use a percent sign and specify the job ID to bring the appropriate one back to the foreground.

Great, now we can put processes into the background and bring them back when we want them later. But what if we want the command to continue executing while it’s in the background? For that, we can use the bg command, followed by an ampersand and the job ID. The following command will make our stopped job resume, while keeping it in the background.

Now we can see that the process is still in the background, but it shows a status of “running” instead of “stopped.”

There’s still one thing to keep in mind. You can’t close your current terminal, or these background jobs will close. If you need to close your terminal, and don’t want these commands to stop, then you need to “disown” the job(s). If you only have one job in the background, the following command will work:

If you have multiple, then you’ll need to specify the job ID.

You’ll no longer see the job in your jobs table when you execute the jobs command. Now it’s safe to close the terminal and your command will continue running.

You can still keep an eye on your running command by using the ps command.

And if you want to stop the command from running, you can use the kill command and specify the process ID.

Start a command in the background

Now we have seen how to put a running process into the background and recall it later. But we also have the option to just start the command in the background at the beginning. All you need to do is put an ampersand at the end of any Linux command.

Just as before, we can see the job listed with the jobs command. This time, though, the job is already running and we didn’t have to manually start it in the background.

Close terminal and keep background job running

We’ve already seen in a previous example how we can use the disown command to ensure that the command continues running after we close our terminal. Another option is the nohup command.

nohup tells a process to ignore any SIGHUP (hangup) signals that it receives. The SIGHUP signal is sent to a background job whenever the current terminal is closed. Thus, we can have a job in the background, close the terminal, and still see it running in the background. Here’s an example.

We have closed the terminal and reopened it, then ran the following command, which shows the command is still running.

Using the screen command

Up until now, we’ve covered the default Linux methods for running commands in the background. However, there’s also extra software packages that can be used. Perhaps the most popular would be the screen command.

You can see the aforementioned link for installation instructions and detailed usage, but the gist of it goes like this:

  1. Run the screen command to start a new “screen”. Optionally, include the -S option to give it a name.
  2. In the new screen session, execute the command or script you wish to put in the background.
  3. Press Ctrl + A on your keyboard, and then D . This will detach the screen, then you can close the terminal, logout of your SSH session, etc, and the screen will persist. To see a list of screens, use this command.

Closing Thoughts

In this guide, we saw several different methods for running a command in the background on Linux. This included default Linux utilities and the screen command. We also saw how to manage background process by moving jobs into the foreground, reattaching screens, and killing background processes.

Process control commands in Unix are:

    bg Command : bg is a process control command that resumes suspended process while keeping them running in the background. User can run a job in the background by adding a “&” symbol at the end of the command.
    Syntax :

Options

The character % introduces a job specification. The Job can be a process ID (PID) number, or we can use one of the following symbol combinations:

bg examples

Command

Output:
fg Command : fg command moves a background job in the current shell environment into the foreground. Use the job ID parameter to indicate a specific job to be run in the foreground. If this parameter is not supplied, the fg command uses the job most recently suspended, placed in the background, or run as a background job .
Syntax :

Options

fg examples

Command

Output:

Command

Output:
Jobs Command : Jobs command is used to list the jobs that you are running in the background and in the foreground. If the prompt is returned with no information no jobs are present. All shells are not capable of running this command. This command is only available in the csh, bash, tcsh, and ksh shells.
Syntax :

Options

jobs command examples

To display the status of jobs in the current shell:
Command

Output:

To display the process ID or jobs for the job whose name begins with “p,”:
Command

Output:

The character % introduces a job specification. In this example, you are using the string whose name begins with suspended command such as %ping.

Pass the -p option to jobs command to display PIDs only:
Command

Output:

Pass the -r option to jobs command to display only running jobs only:
Command

However, Linux allows us to perform tasks to the running processes, including background and foreground jobs.

This tutorial will discuss how to work with background processes and terminate them using various commands.

Let’s get started.

How to Run a Command in the Background

In most cases, when running a command from the Linux terminal, we wait for it to complete and exit. This functionality can be helpful for short commands or commands that require user interaction. However, in the case of processes that take a long time to complete, it can prevent you from running other commands.

Although pseudo-terminals can escape this, this becomes a problem in an sole terminal-based environment.

Use & Symbol

In Linux, one method to run a command in the background is to use the & symbol as:

The above syntax tells the shell to put whatever commands precede the ampersand in the background.

Once you put a process in the background, the shell will give you the job ID enclosed by a pair of square brackets and the PID (process ID).

Use CTRL + Z

Another method to put a process in the background is to use the CTRL + Z shortcut. Suppose we forgot to add the ampersand when running a program.

To put the said process in the background, we can press the CTRL + Z key and suspend the job. It is good to note that this does not terminate the process; it only freezes it.

To resume the process in the background, use the bg command:

How to run and control background processes on linux

As seen in the screenshot above, we run the Firefox process in the foreground, which “eats” our prompt until we terminate the process.

We freeze the process using the CTRL + Z shortcut and put it in the background using the bg command.

How to Show Running (and Stopped) Background Processes

To show the background processes, we use the jobs -l command:

How to run and control background processes on linux

The command will show both the running and stopped processes.

How to Bring a Background Process to the Foreground

To bring a background process in the foreground, you use the fg command followed by %[job id]

How to run and control background processes on linux

How to Kill a Background Process

Killing a background process is fairly straightforward; use the command pkill and the process ID, or process name as:

How to run and control background processes on linux

Using the pkill command will force terminate (-9) the processes with the process name of ping.

Conclusion

This guide walked through the basics of job control using foreground and background processes.

About the author

How to run and control background processes on linux

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list

There are quite a number of ways to look at running processes on Linux systems – to see what’s running, the resources that processes are using, how the system is affected by the load and how memory is being used. Each command gives you a different view, and the range of details is considerable. In this post, we’ll run through a series of commands that can help you view process details in a number of different ways.

While the ps command is the most obvious command for examining processes, the arguments that you use when running ps will make a big difference in how much information will be provided. With no arguments, ps will only show processes associated with your current login session. Add a -u and you’ll see extended details.

Here is a comparison:

Using ps -ef will display details on all of the processes running on the system but ps -eF will add some additional details.

Both commands show who is running the process, the process and parent process IDs, process start time, accumulated run time and the task being run. The additional fields shown when you use F instead of f include:

  • SZ: the process size in physical pages for the core image of the process
  • RSS: the resident set size which shows how much memory is allocated to those parts of the process in RAM. It does not include memory that is swapped out, but does include memory from shared libraries as long as the pages from those libraries are currently in memory. It also includes stack and heap memory.
  • PSR: the processor the process is using

ps -fU

You can list processes for some particular user with a command like “ps -ef | grep USERNAME”, but with ps -fU command, you’re going to see considerably more data. This is because details of processes that are being run on the user’s behalf are also included. In fact, nearly all these processes shown have been kicked off by system simply to support this user’s online session. Nemo has only just logged in and is not yet running any commands or scripts.

Note that the only process with an assigned TTY is Nemo’s shell and that the parent of all of the other processes is systemd.

You can supply a comma-separated list of usernames instead of a single name. Just be prepared to be looking at quite a bit more data.

top and ntop

The top and ntop commands will help when you want to get an idea which processes are using the most resources and allow you to reorder your view depending on what criteria you want to use to rank the processes (e.g., highest CPU or memory use).

Use shift+m to sort by memory use and shift+p to go back to sorting by CPU usage (the default).

A tremendous amount of information is available on running processes in the /proc directory. In fact, if you haven’t visited /proc quite a few times, you might be astounded by the amount of details available. Just keep in mind that /proc is a very different kind of file system. As an interface to kernel data, it provides a view of process details that are currently being used by the system.

Some of the more useful /proc files for viewing include cmdline, environ, fd, limits and status. The following views provide some samples of what you might see.

The status file shows the process that is running (bash), its status, the user and group ID for the person running bash, a full list of the groups the user is a member of and the process ID and parent process ID.

The cmdline file shows the command line used to start the process.

The environ file shows the environment variables that are in effect.

The fd file shows the file descriptors. Note how they reflect the pseudo-tty that is being used (pts/0).

The limits file contains information about the limits imposed on the process.

The pmap command takes you in an entirely different direction when it comes to memory use. It provides a detailed map of a process’s memory usage. To make sense of this, you need to keep in mind that processes do not run entirely on their own. Instead, they make use of a wide range of system resources. The truncated pmap output below shows a portion of the memory map for a single user’s bash login along with some memory usage totals at the bottom.

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as “USL” (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she’s chasing the bears away from her bird feeders.

In the Windows operating system, we have the task manager utility that can be used to see all the currently running processes. However, if you are a Linux user, you might also want to know which processes currently consume your CPU cycles. Fortunately, there are multiple ways in Linux as well through which you can conveniently get the information about all the processes that are currently running on your system. Now, we are going to look at some of these methods.

Methods of Checking the Running Processes in Linux Mint 20

If you ever wish to check all the running processes on your Linux Mint 20 system, you can follow any of these methods that we are just going to discuss.

Method # 1: Using the “ps” Command in Linux Mint 20

The “ps” command in Linux Mint 20 can be used to display all the running processes on the terminal along with their PIDs and some other information as follows:

Here, the “-aux” flag is used to list both the foreground as well as the background running processes.

All the running processes of our system are shown below:

How to run and control background processes on linux

Method # 2: Using the “pstree” Command in Linux Mint 20

Suppose you do not want to be overwhelmed by seeing a large number of running processes as it is on the terminal; rather, you want them to appear in a more visually appealing manner. In that case, you can choose to display all the running processes in Linux Mint 20 in the form of a tree as follows:

All the running processes of our system are shown in the form of a tree below:

How to run and control background processes on linux

Method # 3: Using the “top” Utility in Linux Mint 20

“top” is a built-in utility in the Linux based operating systems that can be used to list down all the running processes on the terminal. Along with the process IDs, this utility also displays some additional information about the running processes such as the username of the user running that process, the CPU and memory consumption of the running process, and free and occupied memory statistics, etc. This command is executed as follows:

All the running processes of our Linux Mint 20 system along with their additional information are shown in the form of a table in the image below:

How to run and control background processes on linux

Whenever you want to exit from the “top” utility interface, you can simply use the “Ctrl+ C” key combination to terminate the processing of this utility.

Method # 4: Using the “htop” Utility in Linux Mint 20

“htop” is another very useful Linux utility that is used to retrieve information about the running processes. “htop” is in fact, an advanced version of the “top” utility which has a better user interface and comparatively better performance. Since the “htop” utility is not installed on a Linux based system by default, therefore, to use it, you will have to install it first with the following command:

This is not a heavyweight utility; therefore, it will only take a few seconds for installation on your Linux Mint 20 system after which your terminal will render the messages shown in the image below:

How to run and control background processes on linux

After this utility is installed on your Linux Mint 20 system, you can easily use it to view all the running processes as follows:

You can see from the output of the “htop” command shown in the image below that it is visually more appealing. The statistics that it reveals about the running processes are more or less the same as that of the “top” utility; however, they are displayed with more catchy colors. Hence, the output looks more fascinating. Also, the speed at which it retrieves these statistics and updates them is better than the “top” utility.

How to run and control background processes on linux

However, just like the “top” utility, whenever you want to exit from the interface of the “htop” utility, you can simply use the “Ctrl+ C” key combination to terminate the processing of this utility.

Conclusion

Today’s discussion revolved around the different methods through which you can check the running processes in Linux Mint 20. These methods can be used depending upon the kind of output you want to retrieve. The first method simply lists all of the running processes at once, whereas the second method presents them in a nice tree-like structure that can be read and processed more easily. The third method follows a table-like structure to display the running processes along with some additional information about them, whereas the fourth one more or less displays the very same information but with a relatively nicer interface. After following this guide, I hope you are now in a position to conveniently check all the running processes in the Linux Mint 20 operating system.

About the author

How to run and control background processes on linux

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.

Concurrent processing is complicated, it’s better to avoid it when possible, but when there is a priority for faster execution or there is a need to have multiple functionalities(programs) simultaneously we have to go for it. Using ampersand(&) symbol we can run processes in background or subshell, this way they run concurrently.When running multiple processes we have to keep track the status of all the processes like their exit codes, stdout, stderr etc..also we should be able to control their life cycle like able to kill them, stop them, pause them etc..

So, to know the exit codes of child process simply store the exit code using “$?” and to know stdout, stderr, we have to redirect the output to a file.

“$?” holds the last executed programs status code.

Example: Monitoring child process status:

Consider the script that just sleeps and spits the exit code to file.

Save the file with name ‘script1.sh’ then make it executable.

Similarly, create another file with script2.sh with following contents.

Now run these two scripts concurrently using ampersand symbol.

In this way the parent process(here the shell session running) can know the exit codes by reading proc1 and proc2 files about the these background process, same way we can redirect the stdout and stderr to files and get to know the status of these child processes.

Example: Controlling child process:

Consider we want to kill the child process, we can do it if we have pid of the child process, so that we can send SIGTERM or SIGKILL signals to kill it. To store the pid of every child process that is going to spawn use “$!” notation, this holds the last run program’s pid.

So in the above scripts replace $? with $!, and run the same command then pids can be read from the files, as we have pids of the child process we have control to stop, kill etc..

Always kill the child process from direct parent:

Linux will reuse pids of dead process, so when we store the pids of child processes of child processes, these grandchildren(not sure of this terminology) may be dead along time ago and you are trying to kill them which could be fatal because the kernel may have assigned those pids to other processes.

However the case is different for direct child processes, the child process that dies before the parent will be considered as zombie process..it doesn’t do anything but its pid is not reaped by the kernel, so the direct parent can send kill signal any no. of times.

Following script creates a child process which runs for a long time.

echo $$ >> pid.txt # current script pid

sleep 315360000 & # I will be alive for 10 years…

echo $! >> pid.txt # 10 years sleep command pid

Name the script long_script.sh and make it executable.

Run: echo $$ > pid.txt; ./long_script &

Content of pid.txt:

1125 – Current terminal shell session pid.

6980 – Child of 1125, the script pid

6981 – Child of 6980, the long sleeping command pid

Here do not try killing 6981 from 1125 process thinking 6981 is the child of 6980, because when 6980 and 6981 processes exit, the kernel will reap the pid 6981 and assign to some other process, but it will not reap 6980 because this process is the direct child of 1125 which is still alive, hence 6980 is tagged as zombie process. So control 6980 child using 1125 parent process and 6981 child process from 6980 parent process.

Exec Command:

When a program is run using ‘exec’ then the current shell is replaced with the program without creating a new process.So when you run ‘exec ’ in a terminal, the terminal shell session exits and replaced by that some_command.This helps is in above scenario when you want to control 6981 process from 1125 which is not a direct parent. To make this happen run the long sleep command with exec, this way sleep command will not create a new process but instead, it will get 6980 as its pid, i.e. the 6980 shell session is replaced by 6981 process.

echo $$ >> pid.txt # current script pid

exec sleep 315360000 # I will be alive for 10 years…

Run: echo $$ > pid.txt; ./long_script &

7414 is current terminal shell session pid, 7696 is the script pid, then script process is replaced by sleep command, so 7696 is the pid of sleep command. Now 7696 is the direct child of 7414 parent, so 7696 can be controlled by 7414.

When Parent Process dies:

When a child’s parent process dies, child processes are considered orphan processes, init process will adopt these orphan processes hence init process becomes the new parent process pid.

To make sure all the child processes die when parent dies we can use prctl PR_SET_PDEATHSIG to inform child when parent dies, so that child knows when the parent dies and exit.

I am reading the bash reference manual about Job control found here (emphasis mine):

To facilitate the implementation of the user interface to job control, the operating system maintains the notion of a current terminal process group ID. Members of this process group (processes whose process group ID is equal to the current terminal process group ID) receive keyboard-generated signals such as SIGINT. These processes are said to be in the foreground. Background processes are those whose process group ID differs from the terminal’s;

I am trying to run a little experiment to confirm this, but I am not getting the results I am expecting.

I have two terminals windows running, (my default terminal is zsh and I am running bash from there, in case this changes anything).

The “experiment” goes as follows:

  1. Run a long process in terminal A, I choose ping -c 4000 google.com
  2. In terminal B, Check the pgid of ping running in terminal A, should be the same as terminal A
  3. Send suspend character in terminal A, now ping should be suspended and added to jobs in terminal A
  4. Check the pgid of ping command, it should be different from the terminal A.

To check the process info I am using the following: ps -e –forest -o pid -o ppid -o comm -o pgid | grep -E ‘bas|zsh|pin’

Actual results
Before staring ping

After staring ping in the foreground:

After sending the suspend signal:

My questions are: How come ping didn’t have the same pgid as its parent terminal ?
How come ping pgid didn’t change when moving from foreground to the background ?

A process means program in execution. It generally takes an input, processes it and gives us the appropriate output. Check Introduction to Process Management for more details about a process. There are basically 2 types of processes.

  1. Foreground processes: Such kind of processes are also known as interactive processes. These are the processes which are to be executed or initiated by the user or the programmer, they can not be initialized by system services. Such processes take input from the user and return the output. While these processes are running we can not directly initiate a new process from the same terminal.
  2. Background processes: Such kind of processes are also known as non interactive processes. These are the processes that are to be executed or initiated by the system itself or by users, though they can even be managed by users. These processes have a unique PID or process if assigned to them and we can initiate other processes within the same terminal from which they are initiated.

Practically Managing the Processes

1. Example of foreground process.

How to run and control background processes on linux

This command will be executed in the terminal and we would be able to execute another command after the execution of the above command.

Note: In this case, the name of the process is sleep 5 but you may change the same as per your need.

2. Stopping a process in between of its execution. To stop a foreground process in between of its execution we may press CTRL+Z to force stop it.

How to run and control background processes on linux

Pressing CTRL+Z in between the execution of the command will stop it.

Note: In this case the name of the process is sleep 100 but you may change the same as per your need.

3. To get the list of jobs that are either running or stopped.

How to run and control background processes on linux

It will display the stopped processes in this terminal and even the pending ones.

4. To run all the pending and force stopped jobs in the background.

How to run and control background processes on linux

This will start the stopped and pending processes in the background.

5. To get details of a process running in background.

How to run and control background processes on linux

Note: In this case the name of the process is sleep 100 but you may change the same as per your need.

6. To run all the pending and force stopped jobs in the foreground.

How to run and control background processes on linux

This will start the stopped and pending processes in the foreground.

7. To run a process in the background without getting impacted by the closing of the terminal.

How to run and control background processes on linux

While executing, it will even store all the output after execution in nohup.out file.

Note: In this case, the process is sleep 100, you may modify it as per your need.

8. To run some processes in the background directly.

How to run and control background processes on linux

This will run the process in the background and will display the process id of the process.
Note:- In this case, the process is sleep 100, you may modify it as per your need.

9. To run processes with priority.

How to run and control background processes on linux

The top priority is -20 but as it may affect the system processes so we have used the priority 5.

Note: In this case, the process is sleep 100, you may modify it as per your need.

10. To get the list of all the running processes on your Linux machine.

How to run and control background processes on linux

This will display all the processes that are currently running in your system.

In this chapter, we will discuss in detail about process management in Unix. When you execute a program on your Unix system, the system creates a special environment for that program. This environment contains everything needed for the system to run the program as if no other program were running on the system.

Whenever you issue a command in Unix, it creates, or starts, a new process. When you tried out the ls command to list the directory contents, you started a process. A process, in simple terms, is an instance of a running program.

The operating system tracks processes through a five-digit ID number known as the pid or the process ID. Each process in the system has a unique pid.

Pids eventually repeat because all the possible numbers are used up and the next pid rolls or starts over. At any point of time, no two processes with the same pid exist in the system because it is the pid that Unix uses to track each process.

Starting a Process

When you start a process (run a command), there are two ways you can run it −

  • Foreground Processes
  • Background Processes

Foreground Processes

By default, every process that you start runs in the foreground. It gets its input from the keyboard and sends its output to the screen.

You can see this happen with the ls command. If you wish to list all the files in your current directory, you can use the following command −

This would display all the files, the names of which start with ch and end with .doc

The process runs in the foreground, the output is directed to my screen, and if the ls command wants any input (which it does not), it waits for it from the keyboard.

While a program is running in the foreground and is time-consuming, no other commands can be run (start any other processes) because the prompt would not be available until the program finishes processing and comes out.

Background Processes

A background process runs without being connected to your keyboard. If the background process requires any keyboard input, it waits.

The advantage of running a process in the background is that you can run other commands; you do not have to wait until it completes to start another!

The simplest way to start a background process is to add an ampersand (&) at the end of the command.

This displays all those files the names of which start with ch and end with .doc

Here, if the ls command wants any input (which it does not), it goes into a stop state until we move it into the foreground and give it the data from the keyboard.

That first line contains information about the background process – the job number and the process ID. You need to know the job number to manipulate it between the background and the foreground.

Press the Enter key and you will see the following −

The first line tells you that the ls command background process finishes successfully. The second is a prompt for another command.

Listing Running Processes

It is easy to see your own processes by running the ps (process status) command as follows −

One of the most commonly used flags for ps is the -f ( f for full) option, which provides more information as shown in the following example −

Here is the description of all the fields displayed by ps -f command −

User ID that this process belongs to (the person running it)

Parent process ID (the ID of the process that started it)

CPU utilization of process

Process start time

Terminal type associated with the process

CPU time taken by the process

The command that started this process

There are other options which can be used along with ps command −

Shows information about all users

Shows information about processes without terminals

Shows additional information like -f option

Displays extended information

Stopping Processes

Ending a process can be done in several different ways. Often, from a console-based command, sending a CTRL + C keystroke (the default interrupt character) will exit the command. This works when the process is running in the foreground mode.

If a process is running in the background, you should get its Job ID using the ps command. After that, you can use the kill command to kill the process as follows −

Here, the kill command terminates the first_one process. If a process ignores a regular kill command, you can use kill -9 followed by the process ID as follows −

Parent and Child Processes

Each unix process has two ID numbers assigned to it: The Process ID (pid) and the Parent process ID (ppid). Each user process in the system has a parent process.

Most of the commands that you run have the shell as their parent. Check the ps -f example where this command listed both the process ID and the parent process ID.

Zombie and Orphan Processes

Normally, when a child process is killed, the parent process is updated via a SIGCHLD signal. Then the parent can do some other task or restart a new child as needed. However, sometimes the parent process is killed before its child is killed. In this case, the “parent of all processes,” the init process, becomes the new PPID (parent process ID). In some cases, these processes are called orphan processes.

When a process is killed, a ps listing may still show the process with a Z state. This is a zombie or defunct process. The process is dead and not being used. These processes are different from the orphan processes. They have completed execution but still find an entry in the process table.

Daemon Processes

Daemons are system-related background processes that often run with the permissions of root and services requests from other processes.

A daemon has no controlling terminal. It cannot open /dev/tty. If you do a “ps -ef” and look at the tty field, all daemons will have a ? for the tty.

To be precise, a daemon is a process that runs in the background, usually waiting for something to happen that it is capable of working with. For example, a printer daemon waiting for print commands.

If you have a program that calls for lengthy processing, then it’s worth to make it a daemon and run it in the background.

The top Command

The top command is a very useful tool for quickly showing processes sorted by various criteria.

It is an interactive diagnostic tool that updates frequently and shows information about physical and virtual memory, CPU usage, load averages, and your busy processes.

Here is the simple syntax to run top command and to see the statistics of CPU utilization by different processes −

Job ID Versus Process ID

Background and suspended processes are usually manipulated via job number (job ID). This number is different from the process ID and is used because it is shorter.

In addition, a job can consist of multiple processes running in a series or at the same time, in parallel. Using the job ID is easier than tracking individual processes.

I need some help regarding job control in linux.

I run my program out of a terminal, like this

My program/process is then being run as the background process.

After a while (let say 5 seconds), I type fg to put my process in the foreground.

After a while (let say 5 seconds), I want my program to go back to the background process and put the bash program as the foreground process, so that I can continue using the terminal.

Here is what I do:

  1. When startng my pprogram, I get the group process id of the foreground process (the bash program) using tcgetpgrp function.
  2. I then wait until my process is the foreground process. I use the tcgetpgrp(STDIN__FILENO) .
  3. I then wait

5 seconds

  • Using tcsetpgrp I put the process which I got in point 1 above, in the foreground.
  • Problem is, I can see that my process is put in the background and the bash program in the foreground, but I cann’t enter command in the terminal. The bash doesn’t “return”, it is still waiting or sleeping.

    I know I can press Ctrl – z and then bg but I don’t want to use that because after Ctrl – z is pressed my process is stopped and that isn’t good because I am running a “real-time” control application. So I want to switch back to the terminal “instantly” without stopping or pausing my program.

    Sorry if my description of my problem seems confusing.

    I have (yet) another wait , & , && control flow question..

    Say I have a script something like this where I want to do as much work at the same time as possible:

    Question 1: In the script, does combine wait for both somethingElse processes to finish while both something processes continue?

    Question 2: If not — and I suspect it doesn’t — how do I get combine to wait only for both somethingElse processes whilst something processes above continue to work away in the background?

    How to run and control background processes on linux

    4 Answers 4

    In your example the combine command will just be run as soon as the subshell exits (and provided the last background process was started without an error). The subshell will exit immediately after the jobs are started since there is no wait command.

    If you want to execute a command based on the return value of two or more simultaneous background processes, then I can’t see any other way other than to use temporary files for the return values. This is because wait can only return the return value of one of the processes it waits for. Also since the background processes must be run in subshells to get their return values at all, they cannnot be stored in variables. You could do:

    If you don’t really care about the return values, you can just start the jobs normally and use wait :

    You can use the wait command:

    You can see the “starting” line happens right away, and the “done” waits for 10 seconds.

    Would process substitution be more efficient, especially if you don’t need to save the files OutputA and OutputB , and care only about Result ? Would this be particularly time-saving because if you have a slow I/O in writing to disk, saving the files OutputA and OutputB might be the rate-limiting step?

    Process substitution allows you to put the command inside instead of saving the output into a file, and then reading from it as input in the “combine” step.

    If memory is a limitation, and the size of outputA and outputB more than what the memory can hold, will it defeat the whole purpose?

    Will combine wait till both processes are completed before it starts running?

    How to run and control background processes on linux

    I actually demonstrate exactly how this sort of thing might be done in another answer here. That answer was to a question about ensuring 2 logs were maintained by a background process, so I demonstrated it with 10.

    Demo Script

    Run Demo

    The above demonstrates. It builds and runs a script named /tmp/script , chmod ‘s it as executable, and runs it in the &background of a &backgrounded ( subshell ) .

    The script rms /tmp/file0-9 10 files and echoes a line every second into all 10 of them. I capture some $info from the disowned process and present it via $(command substitution). While ps still reports on the $pid I capture, I know it still runs so I sleep. When it completes, the lines in all 10 files are counted with wc.

    After you invoke a process in this way you can freely close its original parent process and it will keep on trucking – it’s effectively disowned. This also means you cannot use the conventional wait command, but waiting on ps ‘s return should be more robust in any case.

    Worth mentioning, I think, is that the process is actually initially called in $(command substitution) and printfs me the $info I want so I can effectively control it. But as soon as it drops its terminal output with exec 1>&2 (which is closed in the same subshell with 2>&- ), the process escapes and I have to wait around for it on the other end. Kinda the best of both worlds, especially if you use it to handle input pipes, so long as you can wrap your mind around all of the redirections and process leaders.

    Everything else is just for demonstration here. All you need to run this is the top script and:

    NOTE: This only prints to terminal exactly what I wished to demonstrate it. As noted by the $PPID, this process is disowned by the terminal and is a direct child of $PID 1.

    If you wanted to run two of these concurrently and wait for them you could just hand ps both of their pids and wait.

    Home » SysAdmin » How to Use Linux nohup Command

    SIGHUP (Signal Hang UP) is a signal that terminates a Linux process when its controlling terminal is closed. If you accidentally close a terminal or lose connection with the server, all processes running at the time are automatically terminated.

    Using the nohup command is one way of blocking the SIGHUP signal and allowing processes to complete even after logging out from the terminal/shell.

    In this article, you will learn how to use the nohup command while running processes.

    How to run and control background processes on linux

    nohup Syntax

    The syntax for using the nohup command is:

    nohup Options

    The nohup command features two basic command options, the –help and the –version options.

    To display the help message, run the command:

    How to run and control background processes on linux

    To display version information, type:

    Note: Although nohup may appear similar to daemons, there is a significant difference. Daemons are reserved for processes that continuously run in the background, while nohup is used for processes that take a long time but don’t continue running once done.

    nohup Examples

    There are a number of ways to use the nohup command, including running the required process in the background, running multiple processes simultaneously, or redirecting the output to a different file.

    The examples below explain common use cases for the nohup command.

    1. Running a Process with nohup

    To run a command using nohup without any arguments, simply follow the syntax:

    The shell ignores the output and appends it to the nohup.out file.

    For instance, running the example.sh bash script (which is a simple Hello World script) should prompt the Hello World message in the nohup.out file:

    Verify the contents of the file with:

    2. Running a Process in the Background with nohup

    Running a Linux process in the background frees up the terminal you are working in. To run a Linux process in the background with the nohup command, add the & symbol at the end of the command:

    For example, to run the example.sh bash script in the background, use the command:

    The output displays the shell job ID and process ID – [1] 7366 in the example below.

    To bring the command in the foreground, type:

    The output indicates whether the process is in progress or complete.

    Note: For a list of all the important Linux commands in one place, check out our Linux Commands Cheat Sheet.

    3. Running Multiple Processes in the Background with nohup

    Replace [command1] and [command2] with the commands of your choice. Add more commands if necessary, making sure to separate them with && .

    For instance, to show the date/time and the calendar of the current month, run:

    As the output is directed to the nohup.out file, use the cat command to list the contents of the file and verify the command above:

    The output shows the date and calendar prompted with the command above.

    How to run and control background processes on linux

    4. Redirecting Output to a Different File

    As mentioned in the section above, nohup logs all output messages and errors into the nohub.out file.

    Redirect these messages by specifying a custom location within the command:

    In the example below, the output for the command nohup bash -c ‘date && cal’ is redirected to the output.txt file. Check the output with the command:

    How to run and control background processes on linux

    After reading this article, you should know how to use the nohup command to run processes in the background and redirect their output.

    As an alternative to using the nohup command, consider checking out Tmux. Tmux was built to support multitasking in a terminal window. Learn more in our comprehensive Tmux Tutorial.

    How to run and control background processes on linux

    There’s nothing more annoying than running a command in your terminal and having it run for minutes, sometimes hours, and not be able to use your terminal again. Sure, you can use tabs, but that’s a clunky solution, and it’s not always optimal because you may want to see updates as you’re working. Here we show you a few different ways to run bash commands in the background in Linux.

    Also read: How to Use lsof Command in Linux to List Open Files

    End a Command with &

    If you want to push a command into the background, using & at the end is an easy way to do that. This way, you can issue a command in the background and continue to use your terminal as it runs. It comes with a catch, though. Using & doesn’t disconnect the command away from you; it just pushes it into the background. This means that while you’re trying to use the terminal, anything the command wants to push to STDOUT or STDERR will still be printed, which may be distracting.

    How to run and control background processes on linux

    When the terminal session is closed, the command ends. You can also kill the command by issuing the jobs command, finding the number of the command that’s running, and killing it with the kill command. That syntax is as follows:

    How to run and control background processes on linux

    Using & is good if you need to push something off for a bit but don’t expect it to continue forever.

    Also read: How to Use the dd Command in Linux

    & After a Command, Then Disown It

    Running a command with just & pushes it off to the back and keeps it running as long as the terminal window is open. If, however, you’re looking to keep this command running in constant, even with your terminal session ending, you can use the disown command.

    To use this method, start by adding an & .

    As mentioned above, using & pushes this command into the background but doesn’t detach it from your user. You can verify this by typing jobs into the terminal. It’ll show the command running in the background as we saw before.

    Just type disown into the shell, and it’ll do just that. (And you can once again verify this with the jobs command.)

    How to run and control background processes on linux

    You can just make out the disown command in there

    Now you can close your terminal and continue about your day. It’ll still keep piping things to STDOUT or STDERR , but once you exit and reopen your terminal, you won’t see anything there. You can find the command again with the top or ps commands and kill it with the kill command.

    How to run and control background processes on linux

    The disowned job is the second one, with the PID 16238.

    Also read: How to Set Up Leafnode as an Offline USENET Server

    & After a Command with /dev/null

    Adding & after a command will push a command into the background, but as a result, the background command will continue to print messages into the terminal as you’re using it. If you’re looking to prevent this, consider redirecting the command to /dev/null .

    How to run and control background processes on linux

    This does not prevent the command from closing when the terminal closes. However, as mentioned above, it’s possible to use disown to disown the running command away from the user. You can also kill it in either of the methods mentioned above if you don’t want it to run anymore.

    Also read: The Ultimate Guide to Apt and Apt-Get Commands

    Nohup, with & and /dev/null

    Unlike the previous commands, using nohup allows you to run a command in the background and keep it running. How? nohup bypasses the HUP signal (signal hang up), making it possible to run commands in the background even when the terminal is off. Combine this command with redirection to “/dev/null” (to prevent nohup from making a nohup.out file), and everything goes to the background with one command.

    How to run and control background processes on linux

    Also read: How to Generate SSL Certificates on Linux Using OpenSSL

    Most terminal programs on Linux today have features built in to allow them to run in the background with little effort. Along with that, modern init systems (like systemd) can allow users to start programs like services at boot or whenever.

    Still, some programs on Linux lack the ability to run as a daemon or integrate with modern init systems. This is a real inconvenience but is understandable, as not all developers have the skill or time to add new features.

    Luckily, commands like nohup or disown are still a reality and can close the gap in moving programs like this to the background. They’re not perfect or fancy, but they get the job done when needed.

    If you enjoyed this Linux article, make sure to check out some of our other Linux content, like how to connect your Google account to GNOME Shell, the best Linux distros for windows users, and LS commands you need to know.

    Also read: How to Use cURL for Command Line Data Transfer and More

    Our latest tutorials delivered straight to your inbox

    By How to run and control background processes on linuxPriya Pedamkar

    How to run and control background processes on linux

    Introduction to Linux Process Management

    In Linux, unlike windows, all commands are run on terminal/shell. All administrative tasks can be accomplished through terminal/shell. This includes package installation, navigation, file manipulation, and user management. Process management in Linux is nothing but manipulating (resume, stop or kill) a command which is already in progress, about to start or already killed.

    Linux Process Management

    The process is a program in execution. The process is created when a command is to be executed so, it can be called a running instance of a program in execution. Tuning or controlling a process is called Process Management.

    Web development, programming languages, Software testing & others

    Any process can be run in two ways:

    • Foreground process: By default, All the processes are run in the foreground. When a process is run in foreground, no other process can be run on the same terminal until the process is finished or killed. When issuing this type of process, the system receives input from the keyboard(stdin) and gives output to the screen(stdout).

    How to run and control background processes on linux

    • Background process: Adding ‘&’ to a foreground command makes it a background process. A background process runs on its own without input from the keyboard(stdin) and waits for input from the keyboard. While the process runs in the background, other processes can be run in the foreground.

    How to run and control background processes on linux

    The background process will be in stop state till input from the keyboard is given (usually ‘Enter’ key) then becomes a foreground process and gets executed. Only after the background process becomes a foreground process, that process gets completed else it will be a stop state.

    How to run and control background processes on linux

    Types of Process

    Before knowing about Types of process, Let’s know about basic commands used for process management in Linux.

    Process status(ps): displays all the process in execution

    How to run and control background processes on linux

    To list all process in the background using ‘ps –f’ and to know more info on process use ‘ps -ef’

    How to run and control background processes on linux

    Here’s an example of a list of the process using ‘ps –ef’

    How to run and control background processes on linux

    How to run and control background processes on linux

    • First column: User Id
    • Second column: PID (process Id) – this is the 5-digit number assigned by OS for a process. No PID can be the same.
    • Third column: PPID (parent process Id) – PID of the parent process
    • Fourth column: CPU utilization of process
    • Fifth column: STIME – Process start time
    • Sixth column: TTY – the Terminal type associated with the process
    • Seventh column: CMD – the command that started that process
    • kill: Used to a process whose PID is known. To kill a process forcefully and unconditionally use
    • “kill -9 PID”
    • bg: A job control command that resumes suspended jobs while keeping them running in the background
    • fg: It continues a stopped job by running it in the foreground

    How to run and control background processes on linux

    • top: Another command that shows all the processes running in Linux working environment

    How to run and control background processes on linux

    An example of processes listed by top

    How to run and control background processes on linux

    Now that you are familiar with the important commands used to manage Linux processes, Let’s jump into types of Linux process.

    There are five types of Process in Linux

    1. Parent process: The process created by the user on the terminal. All processes have a parent process, If it was created directly by user then the parent process will be the kernel process.

    How to run and control background processes on linux

    2. Child process: The process created by another process (by its parent process). All child processes have a parent process.

    The example is given above, the process having PID 28500(last row) is a child process of the process having PID 26544.

    3. Orphan process: Sometimes when the parent gets executed before its own child process then the child process becomes an orphan process. The orphan process have “Init” process (PID 0) as their PPID (parent process ID)

    4. Zombie process: The processes which are already dead but shows up in process status is called Zombie process. Zombie processes have Zero CPU consumption.

    5. Daemon process: These are system-related processes that run in the background. A Daemon process can be recognized if it has “?” in its TTY field (6 th column)

    Conclusion

    Linux process management is a fundamental concept you should be knowing if you work on Linux. Now that you know how to manage a Linux process you are one step closer in mastering Linux. To know more about Linux, stay tuned and follow eduCBA.

    Recommended Articles

    This is a guide to Linux Process Management. Here we discuss the basic commands used for process management in Linux and Types of Process. You may also have a look at the following articles to learn more –

    Linux Training Program (16 Courses, 3+ Projects)

    How to run and control background processes on linux

    If you’re working in IT, you might need to schedule various repetitive tasks as part of your automation processes.

    For example, you could schedule a particular job to periodically execute at specific times of the day. This is helpful for performing daily backups, monthly log archiving, weekly file deletion to create space, and so on.

    And if you use Linux as your OS, you’ll use something called a cron job to make this happen.

    What is a cron?

    Cron is a job scheduling utility present in Unix like systems. The crond daemon enables cron functionality and runs in background. The cron reads the crontab (cron tables) for running predefined scripts.

    By using a specific syntax, you can configure a cron job to schedule scripts or other commands to run automatically.

    For individual users, the cron service checks the following file: /var/spool/cron/crontabs

    How to run and control background processes on linuxContents of /var/spool/cron/crontabs

    What are cron jobs in Linux?

    Any task that you schedule through crons is called a cron job. Cron jobs help us automate our routine tasks, whether they’re hourly, daily, monthly, or yearly.

    Now, let’s see how cron jobs work.

    How to Control Access to crons

    In order to use cron jobs, an admin needs to allow cron jobs to be added for users in the ‘/etc/cron.allow’ file.

    If you get a prompt like this, it means you don’t have permission to use cron.

    How to run and control background processes on linuxCron job addition denied for user John.

    To allow John to use crons, include his name in ‘/etc/cron.allow’. This will allow John to create and edit cron jobs.

    Allowing John in file cron.allow

    Users can also be denied access to cron job access by entering their usernames in the file ‘/etc/cron.d/cron.deny’.

    How to Add cron Jobs in Linux

    First, to use cron jobs, you’ll need to check the status of the cron service. If cron is not installed, you can easily download it through the package manager. Just use this to check:

    Cron job syntax

    Crontabs use the following flags for adding and listing cron jobs.

    • crontab -e : edits crontab entries to add, delete, or edit cron jobs.
    • crontab -l : list all the cron jobs for the current user.
    • crontab -u username -l : list another user’s crons.
    • crontab -u username -e : edit another user’s crons.

    When you list crons, you’ll see something like this:

    In the above example,

    • * * * * * represents minute(s) hour(s) day(s) month(s) weekday(s), respectively.
    Value Description
    Minutes 0-59 Command would be executed at the specific minute.
    Hours 0-23 Command would be executed at the specific hour.
    Days 1-31 Commands would be executed in these days of the months.
    Months 1-12 The month in which tasks need to be executed.
    Weekdays 0-6 Days of the week where commands would run. Here, 0 is Sunday.
    • sh represents that the script is a bash script and should be run from /bin/bash .
    • /path/to/script.sh specifies the path to script.

    Below is the summary of the cron job syntax.

    Cron job examples

    Below are some examples of scheduling cron jobs.

    Schedule Scheduled value
    5 0 * 8 * At 00:05 in August.
    5 4 * * 6 At 04:05 on Sunday.
    0 22 * * 1-5 At 22:00 on every day-of-week from Monday through Friday.

    It is okay if you are unable to grasp this all at once. You can practice and generate cron schedules with the crontab guru.

    How to set up a cron job

    In this section, we will look at an example of how to schedule a simple script with a cron job.

    1. Create a script called date-script.sh which prints the system date and time and appends it to a file. The script is shown below:

    How to run and control background processes on linuxScript for printing date.

    2. Make the script executable by giving it execution rights.

    3. Add the script in the crontab using crontab -e .

    Here, we have scheduled it to run per minute.

    Adding a cron job in crontab every minute.

    4. Check the output of the file date-out.txt . According to the script, the system date should be printed to this file every minute.

    How to run and control background processes on linuxOutput of our cron job.

    How to Troubleshoot crons

    Crons are really helpful, but they might not always work as intended. Fortunately, there are some effective methods you can use to troubleshoot them.

    1. Check the schedule.

    First, you can try verifying the schedule that’s set for the cron. You can do that with the syntax you saw in the above sections.

    2. Check cron logs.

    First you need to check if the cron has run at the intended time or not. You can verify this from the cron logs located at var/log/cron . In some distros, logs can be found at /var/log/syslog

    If there is an entry in these logs at the correct time, it means the cron has run according to the schedule you set.

    Below are the logs of our cron job example. Note the first column which shows the timestamp. The path of the script is also mentioned at the end of the line.

    How to run and control background processes on linuxCron job logs.

    3. Redirect cron output to a file.

    You can redirect a cron’s output to a file and check the file for any possible errors.

    Wrapping up

    Automating tasks, like with cron jobs, reduces the repetitive work you need to do. It also lets machines auto heal and work around the clock without human intervention.

    Automation in Linux heavily relies on cron jobs, so you should definitely learn crons and experiment with them.

    Thank you for reading until the end. Feedback is always welcome.

    If you found this article helpful, do share it with your friends.

    By How to run and control background processes on linuxPriya Pedamkar

    How to run and control background processes on linux

    Introduction of Linux Nohup

    In the Linux ecosystem, when a client or user want to logout or sign out from the Linux environment then the sessions or the programs were opened by the user will close or terminated. If the user wants to keep a few programs or applications will open or it will be in running state after the user session will close. Then nohup utility comes in picture and with the help of nohup utility, we can keep the program or application in running state after the user will logout the Linux session.

    The nohup command utility was written by Jim Meyering.

    Web development, programming languages, Software testing & others

    Syntax of Nohup Command

    nohup COMMAND [ARG].
    nohup OPTION

    • nohup : We can use the nohup keyword in the syntax or command. It will work in two different ways i.e. command with arguments and with nohup options. As per the command input, the nohup will keep the job in running state and keep the standard output and error file.
    • COMMAND : We can provide the compatible nohup command.
    • OPTIO : We can provide the different flags as options that arecompatible with the nohup command.
    • ARG : As per the requirement, we can pass the different arguments to nohup command.

    How does Linux Nohup Command Work?

    • In Linux, the nohup command is very suitable for those shell jobs that are taking too much time to execute. In this case, we need to use the nohup utility and execute those long-running shell jobs or applications jobs with the help of nohup utility. The nohup utility will keep the proper track on it and provide all the log related information with the assigned job with it.
    • The nohup command will accept both the command with arguments and nohup option. As per the input, the nohup command will keep the jobs in the background and keep in running state.
    • By default, the standard output of the nohup command will redirect to the nohup.out file. The nohup.out will present in the current working directory.
    • The job will run with the help of nohup utility, the standard error will redirect to the nohup.out file i.e. in the nohup.out the file you will get both the standard output logs as well as the standard error logs.
    • As per the requirement, we can redirect the output log file to different files also except nohup.out file.

    Examples to Implement Linux Nohup Command

    Following are the examples are given below:

    1. NohupCommand

    It is a very simple and common way to use the nohup command in a Linux environment. It will execute any shell or program or command and when the Linux terminal will close still the program or shell job will run. The output logs will update in the nohup.out file.

    Command:

    Explanation: As per the above command, we are using nohup command with the date command. We are providing the command argument as input the nohup command. Once, we will execute the command. It will create the nohup.out file. The file will contain all the logs or command output related information in it (refer screenshot 1 (a)). The nohup.out fill will create automatically, once the job or command will trigger by the nohup command. Kindly find the screenshot 1 (b).

    Output:

    2. Nohup Command – Run Job in Background

    In nohup command, we are having the functionality to run the jobs or commands in the background. To run the jobs in the background, we need to use the “&” with the nohup command.

    Command:

    Explanation: With the help of nohup command, we are able to trigger the job in the background and the keep the proper job tracking with the help of process id (1969) and the nohup.out file. As per the below screenshot 2 (a), we have written one bash job “test_job.sh”. With the help of nohup command, we are executing the bash job and the job will run in the background.

    Once the job will execute, it will display the integer number. The integer number is nothing but a process id. The process id will help to track the backend process. We can also kill the job with the help of the same process id (refer screenshot 2 (b)).

    Last updated October 14, 2021 By marcocarmonaga 8 Comments

    Brief: This is a quick tip on finding zombie processes in Linux and then killing them. You also learn a thing or two about processes and zombie processes.

    Before you learn about Zombie process, let me recall what is a process in Linux.

    In a few words, a process is a running instance of a program in performance. It can be foreground (interactive process) or background (not interactive or automatic process). It can be a parent (creator of other processes during run-time) or child (process created by others) process.

    In Linux, except for the first init (or systemd) process with PID 0, every other process has a parent process. Processes also have their own child processes.

    Don’t believe me? Use the pstree command in terminal to look at the process tree to see the ‘family tree’ of your system’s processes.

    What is a Zombie process in Linux?

    When a child process dies, the parent process is informed so that it can do some clean up like freeing up memory etc. However, child process goes into zombie state if the parent process is not aware of its death. For the parent, the child still exists but the child process is actually dead. This is how zombie processes (also known as defunct processes) are created and stay in the system.

    Here’s an excellent funny take on the zombie process by Turnoff.us:

    ” data-medium-file=”https://itsfoss.com/wp-content/uploads/2021/10/zombies-turnoff-300×175.webp” data-large-file=”https://itsfoss.com/wp-content/uploads/2021/10/zombies-turnoff-800×467.webp” width=”800″ height=”467″ src=”https://itsfoss.com/wp-content/uploads/2021/10/zombies-turnoff-800×467.webp” alt=”zombies turnoff” data-lazy-srcset=”https://itsfoss.com/wp-content/uploads/2021/10/zombies-turnoff-800×467.webp 800w, https://itsfoss.com/wp-content/uploads/2021/10/zombies-turnoff-300×175.webp 300w, https://itsfoss.com/wp-content/uploads/2021/10/zombies-turnoff-768×448.webp 768w, https://itsfoss.com/wp-content/uploads/2021/10/zombies-turnoff.webp 1200w” data-lazy-sizes=”(max-width: 800px) 100vw, 800px” data-lazy-src=”https://itsfoss.com/wp-content/uploads/2021/10/zombies-turnoff-800×467.webp?is-pending-load=1″ srcset=”data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7″> Image credit: Turnoff.us

    Do you really need to worry about Zombie processes?

    Here is important to say that zombie processes are not as dangerous as its name can sound.

    The problem may arise if your system has limited RAM or if there are too many zombie processes eating up RAM. Also, most Linux processes can have maximum PID set to 32768. If there are no available IDs for other productive tasks, your system may crash.

    This rarely happens, but it’s a possibility, specially if a poorly coded program starts inducing numerous zombie processes.

    In such case, it would be a good idea to find and kill zombie process.

    How to find zombie processes?

    A process in Linux can have one of the following states:

    • D = uninterruptible sleep
    • I = idle
    • R = running
    • S = sleeping
    • T = stopped by job control signal
    • t = stopped by debugger during trace
    • Z = zombie

    But where can you see the processes and their respective status? One easy way is to use the terminal and the top command.

    ” data-medium-file=”https://itsfoss.com/wp-content/uploads/2021/10/top-command-view-300×178.png” data-large-file=”https://itsfoss.com/wp-content/uploads/2021/10/top-command-view-800×474.png” loading=”lazy” width=”800″ height=”474″ src=”https://itsfoss.com/wp-content/uploads/2021/10/top-command-view-800×474.png” alt=”top command view” data-lazy-srcset=”https://itsfoss.com/wp-content/uploads/2021/10/top-command-view-800×474.png 800w, https://itsfoss.com/wp-content/uploads/2021/10/top-command-view-300×178.png 300w, https://itsfoss.com/wp-content/uploads/2021/10/top-command-view-768×455.png 768w, https://itsfoss.com/wp-content/uploads/2021/10/top-command-view.png 976w” data-lazy-sizes=”(max-width: 800px) 100vw, 800px” data-lazy-src=”https://itsfoss.com/wp-content/uploads/2021/10/top-command-view-800×474.png?is-pending-load=1″ srcset=”data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7″> Top command show processes and their status

    As you can see in the screenshot above, there are 250 total tasks (or processes), 1 is running, 248 processes are sleeping and 1 is in zombie state.

    Now, the question arises, how to kill the zombie process?

    How to find and kill a zombie process? Can a zombie process be killed?

    How to run and control background processes on linux

    A zombie process is already dead. How do you kill an already dead process?

    In the zombie movies, you shoot the zombies in the head or burn it. That’s not an option here. You can burn your system for killing the zombie process but that’s not a feasible solution 😉

    Some people suggests sending SIGCHLD signal to the parent process. But it is more likely to be ignored. The other option to kill the zombie process is to kill its parent process. That sounds brutal but that’s the only sure shot way of killing zombie processes.

    So, first, let’s list the zombie processes to know their ID. It can be achieved by using the ps command like this in the terminal.

    The 8th column in the output of the ps ux command displays the state of a process. You are asking to print all the matching lines where the state of a process is Z+ (indicating zombie state).

    Once you have identified its process ID, let’s get its parent’s process ID.

    Alternatively, you can combine the above two commands in the following fashion where it directly provides the PID of the zombie process and the PID of its parent process.

    Here you get the parent process ID, so finally kill the process by typing the command line with its respective ID process obtained before.

    killing parent process

    ” data-medium-file=”https://itsfoss.com/wp-content/uploads/2021/10/killing-parent-process-300×68.png” data-large-file=”https://itsfoss.com/wp-content/uploads/2021/10/killing-parent-process-800×180.png” loading=”lazy” width=”800″ height=”180″ src=”https://itsfoss.com/wp-content/uploads/2021/10/killing-parent-process-800×180.png” alt=”killing parent process” data-lazy-srcset=”https://itsfoss.com/wp-content/uploads/2021/10/killing-parent-process-800×180.png 800w, https://itsfoss.com/wp-content/uploads/2021/10/killing-parent-process-300×68.png 300w, https://itsfoss.com/wp-content/uploads/2021/10/killing-parent-process-768×173.png 768w, https://itsfoss.com/wp-content/uploads/2021/10/killing-parent-process.png 976w” data-lazy-sizes=”(max-width: 800px) 100vw, 800px” data-lazy-src=”https://itsfoss.com/wp-content/uploads/2021/10/killing-parent-process-800×180.png?is-pending-load=1″ srcset=”data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7″> Killing parent process

    You can verify if the zombie process is killed or not by running the ps command again or even the top command.

    Congrats! Now you know how to eliminate zombie processes.

    With inputs from Abhishek Prakash.

    Like what you read? Please share it with others.

    In Unix ecosystem, a process is a running program. So, any program running on your Linux box is a process. One or more processes can be running on your Linux box at a time. We can display the information about active processes using ps command. By default, the ps command only shows current user’s processes. You can use “ps -ef” (without quotes, of course) command to display all processes. To view the user of a process, we use “ps -u”. Hope you got a basic idea about Linux processes. Let us now come to the today’s topic – how to change the priority of a process in Linux using nice and renice commands.

    As you might know, by default, Linux kernel considers all processes equally important and allocates the same amount of CPU time for each process. Sometimes, you might want to increase or decrease the priority of certain processes to utilize more CPU time. This is where the nice and renice commands comes in help. Nice command is used to run a process with an user defined priority whereas renice command is used to change the priority of a running process. Generally, nice and renice commands are used to change the priority than the default priority of a process.

    Change The Priority Of A Process With Nice command

    View the default priority of a Process

    Whenever a process starts normally, it gets the default priority value of zero.

    We use cmus program, a command line media player for the purpose of this guide.

    Let us run it using command:

    Note: If a command is terminated by the control operator &, the shell executes the command in the background in a subshell.

    Now let us see the running processes using command:

    Sample output:

    How to run and control background processes on linux

    View running processes using ps command

    As you see in the above output, the niceness value is listed under the column heading “NI”. So, it is clear that all processes gets the default priority value of zero.

    Start a process with nice utility

    If you start a process with nice command without any arguments, it gets the default value of 10. Here 10 is the niceness value or priority. Niceness values range from -20 (most favorable to the process) to 19 (least favorable to the process). To put this simply, the negative values (Eg. -20) gives higher priority to a process and positive values (Eg. 19) gives lower priority.

    Now, let us stop the running program cmus with command:

    Here, 13829 is the PID of cmus program. You will see this value under heading “PID” in the above output.

    Now, run cmus program with ‘nice’ utility.

    Check the running processes with command:

    Sample output:

    How to run and control background processes on linux

    Start a process with nice utility

    See, now cmus process got the default niceness value of 10, which means its priority has been changed.

    Start a process with nice utility with lower priority

    Also, you can start the same process with specific niceness value, for example 15, like below.

    Kill the process with with kill command:

    Again, start the process with niceness value of 15:

    Note: Here, do not confuse (hyphen) with minus. We use (hyphen) to pass a value. Hence, to assign a positive value, we use -15. Likewise, to assign a negative value, we use –15 (i.e double hyphen).

    Let us check the niceness value using ps -al command:

    Sample output:

    How to run and control background processes on linux

    Start a process with nice utility with lower priority

    See, now cmus process got the niceness value of 15.

    In the above examples, we launched the processes with lower priority. Remember positive niceness value represents lower priority.

    Start a Process with Nice utility with higher Priority

    You already know that negative niceness value represents higher priority. Please note that regular users are not allowed to start a process with higher priority. You need to be root user to launch any process with higher priority.

    So, to start a process using nice utility with lower priority, for example -15, run the following command:

    Sample output:

    How to run and control background processes on linux

    Start a Process with Nice utility with higher Priority

    Start processes with -n option

    You can also use -n option to increase or decrease the priority.

    To increase priority, run the following command as root user:

    To decrease priority, run the following command as normal user:

    Change The Priority Of A Running Process With Renice command

    What we have seen so far is we launched a process with an user defined priority. Now, we are going to change the priority of a running process using renice command.

    Let us see the running processes using command:

    Sample output:

    As you see in the above output, cmus is running with lower priority value of 15.

    To change the current niceness value of a running process, for example 18, run the following command:

    Here, 15059 is the process id of cmus process.

    Sample output would be:

    Verify the niceness value using command:

    To change it to negative niceness value (to assign higher priority), run the following command as root user:

    You can even change the priorities of all running group belongs to a particular group like below.

    The above command changes the priority of all running processes which are belongs to a group called ostechnix.

    And also, you can change the priority of running process owned by particular user, for example sk, using command:

    For more options, I suggest you to refer the man pages of nice and renice utilities.

    The UiPath Robot Guide

    This is only available for Attended Robots. When using Unattended Robots to run multiple Processes at the same time, each Running Process consumes a separate license.

    An Attended Robot operates on the same machine as a human. It is usually triggered by user events and should only be run under human supervision. Read more about the Robot’s automation capabilities according to license.

    Background Process Execution is performed by a Robot on a machine for a particular user. Processes run in the same Windows session at the same time.

    Each running process uses its own version of dependencies, even if multiple running processes require a different version of the same dependency. For example, if process A requires dependency v18.4.6 and process B requires dependency v19.4.4, then each process downloads and uses its required version of the dependency.

    According to this type of license, an Attended Robot can concurrently execute one foreground process (with UI interaction) and multiple background processes (without UI interaction).

    A foreground process is used when your automation project needs to interact with UI elements. These processes heavily rely on UIAutomation activities. Please note that you can only execute one foreground process at a time.

    Automation processes that use UIAutomation activities cannot run under a locked screen.

    A background process doesn’t need to interact with UI elements on the screen but rather relies on background processes to pass along information. UIAutomation activities should not be used in these project types. Multiple such processes can simultaneously run on a machine, even if a foreground process is already running.

    The type of process is generally dictated by the type of activities it uses (whether or not they interact with UI elements). However, you can specify the type when you create a new process from Studio. By default, all processes are marked as foreground, unless specified otherwise.

    During start-up, a Windows Session is created for the System Processes to run which are not related to a specific user, this is called Session 0 . This session allows Windows to run system processes needed for the machine itself. When a user logs on to that machine, a new session is created called User Session in which user-specific services run.

    For a more detailed description, the Session 0 Isolation document from Microsoft provides additional information on how sessions are created and information is handled between them.
    Using background processes offers a different behavior when they are run in Attended vs Unattended automations. Background Processes started from an Attended Robot run in the user’s session ( Session 1 ) while the ones started from Unattended Robots run in the Windows Session.

    Background Process Automation with Attended Robots rely on the user’s session on the machine to run automations, these are started from Studio or UiPath Assistant and run in the same Windows Session as the user. As the automation happens in the same session, the robot is able to retrieve information and access files that are specific to the user.

    Background Process Automation with Unattended Robots are started from via the Robot Service and run in the Windows Session (Session 0). It’s important to know that Session 0 has no User Interface and cannot interact with a user session. When using this type of processes on Unattended Robots keep in mind that they run in the name of the user and they inherit its permissions. Make sure that the user under which the process runs has access to the needed resources and that the process does not require any type of User Interface. See the Windows Session document for more details on how windows sessions are being handled by the UiPath Robots.

    When running Background Automations via Unattended Robots, using Microsoft Offce applications such as Word, Excel or PowerPoint might not work properly as they have been built to run in User-Session, meaning that they need an interactive desktop session and user profile. For additional information on how Office 365 Products work with Server Sessions(session 0), see the Server-side Automation of Office document from Microsoft.

    A Background Process can be transitioned to Foreground with the help of Use Foreground Activity. More details on how this activity works and how to use it in building your automation can be found here.

    As long as at least one process is running, the Robot is marked as Busy in Orchestrator.

    Starting with the 2021.10 release, running unattended background automations on service-mode robots run by default under the built-in “Local Service” Windows user. Prior to this, the username and password configured for the robot in Orchestrator were used.

    To use credentials specified in Orchestrator, you need to configure the UIPATH_HEADLESS_WITH_USER environment variable on the machine and set the value to True .
    Running automations under the Local Service account is lightweight as it does not need login credentials set in Orchestrator. This has the following limitations:

    • In automation workflows, access is limited to Local Service account privileges.
    • Proxy configurations for the executor are not supported.

    Starting another foreground process while one is already running is not possible. Background processes, on the other hand, can be started regardless of the state and type of the other processes.

    You can use the Run Parallel Process activity in your background processes to start other processes, based on triggers you need. Processes started by this activity run independently. For example, a background process which monitors particular actions can start a new process (foreground or background) and continue to monitor the specific action without being interrupted. A background process loaded by this activity starts right away, even if a foreground and one or more background processes are already running. If the activity starts a foreground process, one of the following situations occurs:

    • The process starts right away if there is no other foreground process running.
    • An exception is thrown if a foreground process is already running.

    Whats a job in Linux

    A job is a process that the shell manages. Each job is assigned a sequential job ID. Because a job is a process, each job has an associated PID. There are three types of job statuses:
    1. Foreground: When you enter a command in a terminal window, the command occupies that terminal window until it completes. This is a foreground job.
    2. Background: When you enter an ampersand (&) symbol at the end of a command line, the command runs without occupying the terminal window. The shell prompt is displayed immediately after you press Return. This is an example of a background job.
    3. Stopped: If you press Control + Z for a foreground job, or enter the stop command for a background job, the job stops. This job is called a stopped job.

    Job Control Commands

    Job control commands enable you to place jobs in the foreground or background, and to start or stop jobs. The table describes the job control commands.

    Option Description
    jobs Lists all jobs
    bg %n Places the current or specified job in the background, where n is the job ID
    fg %n Brings the current or specified job into the foreground, where n is the job ID
    Control-Z Stops the foreground job and places it in the background as a stopped job

    Running a Job in the Background

    To run a job in the background, you need to enter the command that you want to run, followed by an ampersand (&) symbol at the end of the command line. For example, run the sleep command in the background.

    The shell returns the job ID, in brackets, that it assigns to the command and the associated PID. With the job ID, you can use the job control commands to manage the job whereas the kernel uses PIDs to manage jobs.

    When a background job is complete and you press Return, the shell displays a message indicating the job is done.

    Managing the background jobs

    You can use the jobs command to list the jobs that are currently running or suspended in the background.

    You can use the fg command to bring a background job to the foreground.

    You can use the ‘Control+Z keys and bg command to return a job to the background. The Control+Z keys suspend the job, and place it in the background as a stopped job. The bg command runs the job in the background. For example:
    1. Using CTRL+Z

    Updated April 23, 2022

    In this tutorial, you will learn-

    • What is a Process?
    • Running a Foreground Process
    • Running a Background process
    • Fg
    • Top
    • PS
    • Kill
    • NICE
    • DF
    • Free

    What is a Process?

    An instance of a program is called a Process. In simple terms, any command that you give to your Linux machine starts a new process.

    How to run and control background processes on linux

    Having multiple processes for the same program is possible.

    Types of Processes:

    • Foreground Processes: They run on the screen and need input from the user. For example Office Programs
    • Background Processes: They run in the background and usually do not need user input. For example Antivirus.

    Click here if the video is not accessible

    Running a Foreground Process

    To start a foreground process, you can either run it from the dashboard, or you can run it from the terminal.

    When using the Terminal, you will have to wait, until the foreground process runs.

    How to run and control background processes on linux

    Running a Background process

    If you start a foreground program/process from the terminal, then you cannot work on the terminal, till the program is up and running.

    Particular, data-intensive tasks take lots of processing power and may even take hours to complete. You do not want your terminal to be held up for such a long time.

    To avoid such a situation, you can run the program and send it to the background so that terminal remains available to you. Let’s learn how to do this –

    How to run and control background processes on linux

    You can use the command “fg” to continue a program which was stopped and bring it to the foreground.

    The simple syntax for this utility is:

    1. Launch ‘banshee’ music player
    2. Stop it with the ‘ctrl +z’ command
    3. Continue it with the ‘fg’ utility.

    How to run and control background processes on linux

    Let’s look at other important commands to manage processes –

    This utility tells the user about all the running processes on the Linux machine.

    How to run and control background processes on linux

    Press ‘q’ on the keyboard to move out of the process display.

    The terminology follows:

    Can be 20(highest) or -20(lowest)

    There are five types:

    ‘D’ = uninterruptible sleep

    ‘T’ = traced or stopped

    This command stands for ‘Process Status’. It is similar to the “Task Manager” that pop-ups in a Windows Machine when we use Cntrl+Alt+Del. This command is similar to ‘top’ command but the information displayed is different.

    To check all the processes running under a user, use the command –

    You can also check the process status of a single process, use the syntax –

    This command terminates running processes on a Linux machine.

    To use these utilities you need to know the PID (process id) of the process you want to kill

    To find the PID of a process simply type

    Let us try it with an example.

    Linux can run a lot of processes at a time, which can slow down the speed of some high priority processes and result in poor performance.

    To avoid this, you can tell your machine to prioritize processes as per your requirements.

    This priority is called Niceness in Linux, and it has a value between -20 to 19. The lower the Niceness index, the higher would be a priority given to that task.

    The default value of all the processes is 0.

    To start a process with a niceness value other than the default value use the following syntax

    If there is some process already running on the system, then you can ‘Renice’ its value using syntax.

    To change Niceness, you can use the ‘top’ command to determine the PID (process id) and its Nice value. Later use the renice command to change the value.

    Let us understand this by an example.

    How to run and control background processes on linux

    This utility reports the free disk space(Hard Disk) on all the file systems.

    If you want the above information in a readable format, then use the command

    How to run and control background processes on linux

    This command shows the free and used memory (RAM) on the Linux system.

    Interactive processes are initialized and controlled through a terminal session. In other words, there has to be someone connected to the system to start these processes; they are not started automatically as part of the system functions. These processes can run in the foreground, occupying the terminal that started the program, and you can’t start other applications as long as this process is running in the foreground. Alternatively, they can run in the background, so that the terminal in which you started the program can accept new commands while the program is running. Until now, we mainly focussed on programs running in the foreground – the length of time taken to run them was too short to notice – but viewing a file with the less command is a good example of a command occupying the terminal session. In this case, the activated program is waiting for you to do something. The program is still connected to the terminal from where it was started, and the terminal is only useful for entering commands this program can understand. Other commands will just result in errors or unresponsiveness of the system.

    While a process runs in the background, however, the user is not prevented from doing other things in the terminal in which he started the program, while it is running.

    The shell offers a feature called job control which allows easy handling of multiple processes. This mechanism switches processes between the foreground and the background. Using this system, programs can also be started in the background immediately.

    Running a process in the background is only useful for programs that don’t need user input (via the shell). Putting a job in the background is typically done when execution of a job is expected to take a long time. In order to free the issuing terminal after entering the command, a trailing ampersand is added. In the example, using graphical mode, we open an extra terminal window from the existing one:

    The full job control features are explained in detail in the bash Info pages, so only the frequently used job control applications are listed here:

    Table 4-1. Controlling processes

    (part of) command Meaning
    regular_command Runs this command in the foreground.
    command & Run this command in the background (release the terminal)
    jobs Show commands running in the background.
    Ctrl+ Z Suspend (stop, but not quit) a process running in the foreground (suspend).
    Ctrl+ C Interrupt (terminate and quit) a process running in the foreground.
    %n Every process running in the background gets a number assigned to it. By using the % expression a job can be referred to using its number, for instance fg %2.
    bg Reactivate a suspended program in the background.
    fg Puts the job back in the foreground.
    kill End a process (also see Shell Builtin Commands in the Info pages of bash)

    More practical examples can be found in the exercises.

    Most UNIX systems are likely to be able to run screen, which is useful when you actually want another shell to execute commands. Upon calling screen, a new session is created with an accompanying shell and/or commands as specified, which you can then put out of the way. In this new session you may do whatever it is you want to do. All programs and operations will run independent of the issuing shell. You can then detach this session, while the programs you started in it continue to run, even when you log out of the originating shell, and pick your screen up again any time you like.

    This program originates from a time when virtual consoles were not invented yet, and everything needed to be done using one text terminal. To addicts, it still has meaning in Linux, even though we’ve had virtual consoles for almost ten years.