[Bash] job control

Bash is built to handle multiple programs running in parallel.

time cat

Type time cat and then hit ctrl-c before one second, as close as possible without going over:

$ time cat
^C

real    0m0.920s
user    0m0.004s
sys 0m0.000s

ctrl-c

Terminate a process in the foreground.

ctrl-z

Put a process in the background.

fg JOB

Move a process from the background to the foreground by its JOB.

~ $ cat
^Z
[1]+  Stopped                 cat
~ $ echo wow
wow
~ $ fg %1
cat
cool
cool

You can type jobs to see what jobs are running.

If you just type fg, it brings back the last job, or you do fg %1 which brings back the first job.

kill

You can kill the program

# kill the second job
kill -9 %2

job syntax

When you background a process with ctrl-z, the shell prints a message with [N]. N is the job id. Use %N to refer to a particular job or:

%% - the most recent job
& - Another way to background a process

$ ~ node &
[1] 29877

The job id of node is 1 and the process id is 29877. Job ids are local to a shell session, but process ids are global across the entire system.

~ $ perl &
[1] 29870
~ $ pgrep perl
29870
~ $ kill %1
[1]+  Terminated              perl

pgrep

Seach for a process by its name

pkill

Kill the program by its name

# run a watch cmd
$ watch -n1 date

# grep the watch cmd
$ pgrep watch -lf

# kill the watch cmd
$ pkill watch
posted @ 2024-06-26 17:54  Zhentiw  阅读(7)  评论(0编辑  收藏  举报