Cookie

Send a process in the background and foreground with Linux

Published by TheJoe on

Estimated reading time: 2 minutes

Caution


This article was published more than a year ago, there may have been developments.
Please take this into account.

A few articles ago we talked about screen: a command line application that allows you to create virtual sessions containing virtual terminal windows to which we can connect or disconnect at will via the current terminal session.

Screen is a very powerful program, intuitive after a few uses, which can do many things (and in fact the shortcuts are many and too many to list), but today we talk about the classic sessions.

Also in this case a command can last several minutes, or even hours. A system is therefore needed to free up the terminal and execute new commands, leaving the old ones running in the background.

Start a background process

Suppose we have a process that, once done, occupies the terminal session and prevents us from executing other commands until the end of the execution. For example, in case your data disk is large and full of data updatedb it can take a really long time to complete the file index.

~# updatedb

We have two solutions.

Use the “and – commercial” (&)

The first method is to simply add a & at the end of the command. The process will automatically go into the background, the shell will be freed and can be used immediately.

~# updatedb &
[1] 342824
~#

In this scenario, as well as making the terminal available right away, the command returns the process identification number (PID) with which we can monitor the execution status through the command ps.

~# ps ax | grep updatedb
342824 pts/2 S 0:00 /bin/sh /usr/bin/updatedb
~#

Use the shortcut Ctrl+Z and command bg

This method is even more interesting than the first, because even if we don't expect the command it may freeze the terminal for a long time, we will be able to pause the process and send it to the background to continue execution.

~# updatedb
^Z                       # Here i clicked Ctrl+Z
[2]+ Fermato updatedb
~# bg
[2]+ updatedb &
~#

Above I ran the command again updatedb, I paused it with the shortcut Ctrl+Z, I ran the command bg to put the paused command in the background.

Look here:  Mount the local remote FTP with "curlftpfs"

A list of background processes

It, but I don't want to lose control. How many and which processes I sent to the background?

:~# jobs
[1]- In esecuzione sleep 500 &
[2]+ In esecuzione updatedb &
~#

To resume a process we use the command fg together with the number relating to the process we want to restore.

~# jobs
[1]-  In esecuzione           sleep 500 &
[2]+  In esecuzione           updatedb &
~# fg %2
updatedb

Of course we can put the command just restored in the background again with Ctrl+Z and bg closing us in an eternal loop.

Make a process persist even when the shell is closed

When a process has been backgrounded by a session, when the session is closed, the process will also terminate. This aspect can be a problem especially in the case of unstable connections (SSH).

The simplest thing in such cases is to use screen, but in general if this is not what we want we can make a process persist even after session close simply from session with command nohup.

This command is immune to hangups and can ignore SIGHUP signals issued to a process.

The command nohup it must be used before the process we want “disconnect”.

~# nohup updatedb &

Now we can end the current session, open another terminal and check that the process is still running with ps ax | grep updatedb.


TheJoe

I keep this blog as a hobby by 2009. I am passionate about graphic, technology, software Open Source. Among my articles will be easy to find music, and some personal thoughts, but I prefer the direct line of the blog mainly to technology. For more information contact me.

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.