1

We can use & to make the bash execute in background for example, livereload &, php -S localhost:8000 &, but if I left the terminal and forgot to kill the procedure, it will always be there until reboot, I can check which bash is executed by me by using command jobs, but if I open a new terminal or login again, jobs -l will be empty, who to find it out when login another time.

Chan
  • 1,947
  • 6
  • 25
  • 37

1 Answers1

4

If you use &, this places the process in the background; however, that process is still attached to that specific terminal (more specifically, the active shell that the command was executed in). When you exit that terminal (or logout of that shell), the process gets killed. (See: https://unix.stackexchange.com/questions/86247/what-does-ampersand-mean-at-the-end-of-a-shell-script-line)

A situation where the process will not get killed upon exiting that shell is when the process forked a child process, and that child process is now owned by init (PPID 1). This is likely the case with livereload &.

You can also place a process currently running in the foreground to the background by hitting Ctrl-Z, which will bring you back to your shell, while suspending (not killing) the process, and then typing bg, which will then resume executing the process in the background.

When you've run <cmd> &, or when you've moved a process to the background with Ctrl-z and bg, you can bring it back to the foreground by typing fg. At this point, you'll be able to kill it with Ctrl-c.

In general, though, to find the process running that has been detached, you can use

ps -f | grep <name of the command> | grep -v grep

If you want to see other users' processes, you'll want to add -e and -a as in

ps -eaf | grep <name of the command> | grep -v grep

or

ps aux | grep <name of the command> | grep -v grep

(Note: the grep -v grep part will remove the process that executed the grep command.)

Now, to detach a process from your shell, you can use one of a few utilities:

Edit: If you don't remember what you placed in the background, and those processes have detached from your current shell (or potentially are catching the hangup signal), you can grep from your history and see if you can find that in your active processes with

history | grep "&$" | sed "s/&//g" | sed "s/^[ \t]*[0-9]*//g" | sed "s/^[ \t]*//g" | xargs -d '\n' pgrep

The way this one-liner works is it greps your history for anything with an & at the end of it -- e.g.,

898 my_script &   # <----- you'll only see lines like this
899 ls
900 sleep 10000 & # <----- you'll only see lines like this
901 pwd

then it passes that to sed and removes the trailing ampersand. It passes it two more times through sed to remove the starting number (i.e., the number of the command in your history) and starting whitespace. Then it greps the active processes using pgrep. The output (for me, for sleep 10000 & was 90448

mgr@tesla:~$ ps aux | grep sleep
mgr             91040   0.0  0.0  2434836    756 s002  S+    9:37AM   0:00.00 grep sleep
mgr             90448   0.0  0.0  2434820    340 s002  S     9:24AM   0:00.00 sleep 100000
mgr@tesla:~$ history | grep "&$" | sed "s/&//g" | sed "s/^[ \t]*[0-9]*//g" | sed "s/^[ \t]*//g" | xargs pgrep
90448

Now, you could even pipe this into xargs again to kill those processes (if you are confident that they are correct). If you want to verify, you can just run

mgr@tesla:~$ ps -p 90448 -o comm=
sleep

or

mgr@tesla:~$ ps aux | grep 90448 | grep -v grep
mgr             90448   0.0  0.0  2434820    340 s002  S     9:24AM   0:00.00 sleep 100000
Community
  • 1
  • 1
Michael Recachinas
  • 2,739
  • 2
  • 20
  • 29
  • I am sure I run livereload or something else, I can still find it when login again with `ps aux | grep livereload`, it's not closed, I know `ps` with `grep` can find it, but I am looking for something like jobs can show that at once in case I forgot what I ran before – Chan Jan 20 '16 at 05:44
  • This may be useful: http://unix.stackexchange.com/questions/104821/how-to-stop-a-background-process – Michael Recachinas Jan 20 '16 at 05:47
  • `jobs` will only show background commands started from and belonging to the same shell. – Michael Recachinas Jan 20 '16 at 05:58
  • Yup I know, that's what I am asking here – Chan Jan 20 '16 at 06:06
  • I'll stress that `livereload` is likely the exception here, not the rule. When you place something in the background with `&`, it is connected to that shell session and will be killed when you exit that specific shell. In the case of livereload, it might suffice to grep through your history and look for cases where you placed a process in the background, and then grep for that in `ps aux` or `ps -f`. If you have any other examples of cases where processes detach with a simple `&`, do provide them. Otherwise, this sounds like a `livereload`-specific case here. – Michael Recachinas Jan 20 '16 at 14:08