使用&进行后台运行
演示
编写一个shell脚本,每5s打印一次系统时间
让该程序后台运行,并存储运行输出到指定文件
nohup bash print_date.sh > out.file 2>&1 &
代码如下所示:
[root@localhost ~]# cd shell_test1
[root@localhost shell_test1]# touch cc.sh
[root@localhost shell_test1]# vim cc.sh
进入脚本编辑内容
#!/bin/bash
while true
do
sleep 5
date
done
[root@localhost shell_test1]# chmod a+x cc.sh
[root@localhost shell_test1]# ./cc.sh
Wed Apr 21 15:30:32 CST 2021
Wed Apr 21 15:30:37 CST 2021
Wed Apr 21 15:30:42 CST 2021
^C
[root@localhost shell_test1]# nohup bash cc.sh > out.txt 2>&1 &
[3] 5463
[root@localhost shell_test1]# cat out.txt
nohup: ignoring input
Wed Apr 21 16:01:11 CST 2021
Wed Apr 21 16:01:16 CST 2021
Wed Apr 21 16:01:21 CST 2021
Wed Apr 21 16:01:26 CST 2021
Wed Apr 21 16:01:31 CST 2021
Wed Apr 21 16:01:36 CST 2021
Wed Apr 21 16:01:41 CST 2021
Wed Apr 21 16:01:46 CST 2021
Wed Apr 21 16:01:51 CST 2021
Wed Apr 21 16:01:56 CST 2021
Wed Apr 21 16:02:01 CST 2021
Wed Apr 21 16:02:06 CST 2021
Wed Apr 21 16:01:07 CST 2021
Wed Apr 21 16:01:12 CST 2021
Wed Apr 21 16:01:17 CST 2021
Wed Apr 21 16:01:22 CST 2021
Wed Apr 21 16:01:27 CST 2021
Wed Apr 21 16:01:32 CST 2021
Wed Apr 21 16:01:37 CST 2021
Wed Apr 21 16:01:42 CST 2021
Wed Apr 21 16:01:47 CST 2021
Wed Apr 21 16:01:53 CST 2021
Wed Apr 21 16:01:58 CST 2021
Wed Apr 21 16:02:03 CST 2021
[root@localhost shell_test1]# ps -aux | grep cc.sh
root 340 0.0 0.1 113172 1372 pts/1 S 15:56 0:00 bash cc.sh
root 5463 0.0 0.1 113172 1364 pts/1 S 16:01 0:00 bash cc.sh
root 5579 0.0 0.0 112708 980 pts/0 R+ 16:01 0:00 grep --color=auto cc.sh
最后如何结束进程?
kill -9 进程号