在linux后台执行脚本

1. 使用&符号在后台执行命令

你可以在Linux命令或者脚本后面增加&符号,从而使命令或脚本在后台执行,例如:.

$ ./my-shell-script.sh &

2. 使用nohup在后台执行命令

使用&符号在后台执行命令或脚本后,如果你退出登录,这个命令就会被自动终止掉。要避免这种情况,你可以使用nohup命令,如下所示:

$ nohup ./my-shell-script.sh &

3. 使用screen执行命令

通过nohup和&符号在后台执行命令后,即使你退出登录,这个命令也会一直执行。但是,你无法重新连接到这个会话,要想重新连接到这个会话,你可以使用screen命令。.

Linux的screen命令提供了分离和重新连接一个会话的功能。当你重新连接这个会话的时候,你的终端和你分离的时候一模一样。

4. 使用at将一个命令作为批处理执行

使用at命令,你可以让一个命令在指定的日期和时间运行,例如要在明天上午10点在后台执行备份脚本,执行下面的命令:

$ at -f backup.sh 10 am tomorrow

在批处理模式下执行某些任务需要启用一些选项。下面的文章会给出详细解释:.

5. 使用watch连续地执行一个命令

要想按一个固定的间隔不停地执行一个命令,可以使用watch命令,如下所示:

$ watch df -h

原文链接:http://www.cnblogs.com/Javame/p/3582885.html

测试:
[root@rusky ~]# nohup ping 192.168.1.100 > /tmp/test_nohup_ping.txt &
[1] 2619
[root@rusky ~]# nohup: ignoring input and redirecting stderr to stdout

[root@rusky ~]# nohup ping 192.168.1.202 > /tmp/test_nohup_ping2.txt &
[2] 2628
[root@rusky ~]# nohup: ignoring input and redirecting stderr to stdout

[root@rusky ~]# nohup ping 127.0.0.1 > /tmp/test_nohup_ping3.txt &
[3] 2629
[root@rusky ~]# nohup: ignoring input and redirecting stderr to stdout

[root@rusky ~]# jobs
[1]   Running                 nohup ping 192.168.1.100 > /tmp/test_nohup_ping.txt &
[2]-  Running                 nohup ping 192.168.1.202 > /tmp/test_nohup_ping2.txt &
[3]+  Running                 nohup ping 127.0.0.1 > /tmp/test_nohup_ping3.txt &
[root@rusky ~]# 

ctrl+z 停止进程 ctrl+c 终止进程

[root@rusky ~]# fg
nohup ping 127.0.0.1 > /tmp/test_nohup_ping3.txt
^Z
[3]+  Stopped                 nohup ping 127.0.0.1 > /tmp/test_nohup_ping3.txt
[root@rusky ~]# jobs
[1]   Running                 nohup ping 192.168.1.100 > /tmp/test_nohup_ping.txt &
[2]-  Running                 nohup ping 192.168.1.202 > /tmp/test_nohup_ping2.txt &
[3]+  Stopped                 nohup ping 127.0.0.1 > /tmp/test_nohup_ping3.txt

 bg:Resume  each  suspended  job jobspec in the background, as if it had been started with &.

 fg: Resume  jobspec in the foreground, and make it the current job.

这个'+'就是表示在当前窗口下后台默认调用的任务。如下,输入fg,调用的是[3]job,也就是带+号的job。

job  job编号,调用指定的job

[root@rusky ~]# fg
nohup ping 127.0.0.1 > /tmp/test_nohup_ping3.txt
^Z
[3]+  Stopped                 nohup ping 127.0.0.1 > /tmp/test_nohup_ping3.txt
[root@rusky ~]# job 1  
bash: job: command not found...
[root@rusky ~]# fg 1
nohup ping 192.168.1.100 > /tmp/test_nohup_ping.txt
^C[root@rusky ~]# jobs
[2]-  Running                 nohup ping 192.168.1.202 > /tmp/test_nohup_ping2.txt &
[3]+  Stopped                 nohup ping 127.0.0.1 > /tmp/test_nohup_ping3.txt

bg %jobnumber(%可省略)命令激活job3

[root@rusky ~]# jobs
[2]-  Running                 nohup ping 192.168.1.202 > /tmp/test_nohup_ping2.txt &
[3]+  Stopped                 nohup ping 127.0.0.1 > /tmp/test_nohup_ping3.txt
[root@rusky ~]# 
[root@rusky ~]# bg 3
[3]+ nohup ping 127.0.0.1 > /tmp/test_nohup_ping3.txt &
[root@rusky ~]# jobs
[2]-  Running                 nohup ping 192.168.1.202 > /tmp/test_nohup_ping2.txt &
[3]+  Running                 nohup ping 127.0.0.1 > /tmp/test_nohup_ping3.txt & 

jobs -l :    List process IDs in addition to the normal information.

[root@rusky ~]# jobs -l
[1]   2619 Running                 nohup ping 192.168.1.100 > /tmp/test_nohup_ping.txt &
[2]-  2628 Running                 nohup ping 192.168.1.202 > /tmp/test_nohup_ping2.txt &
[3]+  2629 Stopped                 nohup ping 127.0.0.1 > /tmp/test_nohup_ping3.txt
[root@rusky ~]# 

这样,我们可以用Kill命令加jobs的ID号杀死进程

原文地址:https://www.cnblogs.com/rusking/p/5915672.html