Cli Tips

find

strace bash
strace ls

adamf@kid-charlemagne:/usr/src/bash-4.0/bash-4.0$ find . | xargs grep -n "execve ("

How to Start Linux Command in Background and Detach Process in Terminal

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.

$ tar -czf home.tar.gz .
$ bg
$ jobs

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

$ tar -czf home.tar.gz . &
$ jobs

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.

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.

$ sudo rsync Templates/* /var/www/html/files/ &
$ jobs
$ disown  -h  %1
$ jobs

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

$ nohup tar -czf iso.tar.gz Templates/* &
$ jobs
Written on April 7, 2018