后台运行Shell脚本

使用&进行后台运行

演示
编写一个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 进程号

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值