[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 @   Zhentiw  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2023-06-26 [Web] HTTP1,2,3
2023-06-26 [Javascript] this: What get logged?
2023-06-26 [Javascript] Async await in Call stack
2020-06-26 [ML L3] SVM Intro
2019-06-26 [Functional Programming] Partten: When Object props satisfies function condition then do something
2019-06-26 [Algorithm] Martrix Spirals
2016-06-26 [Javascript] Ex: concatAll, map and filter
点击右上角即可分享
微信分享提示