在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
在批处理模式下执行某些任务需要启用一些选项。下面的文章会给出详细解释:.
- How To Capture Unix Top Command Output to a File in Readable Format
- Unix bc Command Line Calculator in Batch Mode
- How To Execute SSH and SCP in Batch Mode (Only when Passwordless login is enabled)
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号杀死进程
在linux后台执行脚本的更多相关文章
- Linux后台执行脚本 &与nohup
Linux后台执行脚本的方式: 0.脚本代码 [root@VM_1_3_centos apps]# cat test.php <?php sleep(5); echo "hello w ...
- Linux后台执行脚本文件,nohup
看运维人员执行nohup命令后,把程序放在后台执行,很高大上,就研究了一下,这个命令. nohup命令及其输出文件 nohup命令:如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,那么 ...
- Linux后台执行命令:&和nohup nohup和&后台运行,进程查看及终止
nohup和&后台运行,进程查看及终止 阅读目录 nohup和&后台运行,进程查看及终止 1.nohup 2.& 3.nohup和&的区别 &:是指在后台运 ...
- 使用php作linux自动执行脚本
使用php作linux自动执行脚本 [来源] 达内 [编辑] 达内 [时间]2013-03-21 在作社区时, 时常需要统计上线人数等数据. 一般做法是, 把这段代码放在用户 login或者 ...
- linux-ssh远程后台执行脚本-放置后台执行问题(转)
写了一个监控负载的小脚本(死循环,测试结束后再kill对应进程),因需要监控多台服务器,所以在一台服务器上使用ssh统一执行脚本遇到问题:使用ssh root@172.16.146.20 '/usr/ ...
- Linux后台执行
在Linux中有时你须要将脚本(test.sh)和可运行程序(exe)后台运行,请使用例如以下方式: nohup ./test.sh & nohup ./exe & 这样执行的程序能够 ...
- Linux后台执行的方法 - 关闭、退出不影响
=============================================================================================nohup c ...
- linux后台执行命令:&和nohup
当我们在终端或控制台工作时,可能不希望由于运行一个作业而占住了屏幕,因为可能还有更重要的事情要做,比如阅读电子邮件.对于密集访问磁盘的进程,我们更希望它能够在每天的非负荷高峰时间段运行(例如凌晨).为 ...
- 【liunx】linux后台执行命令:&和nohup
当我们在终端或控制台工作时,可能不希望由于运行一个作业而占住了屏幕,因为可能还有更重要的事情要做,比如阅读电子邮件.对于密集访问磁盘的进程,我们更希望它能够在每天的非负荷高峰时间段运行(例如凌晨).为 ...
随机推荐
- 【USACO 2.3.2】奶牛家谱
[题目描述] 农民约翰准备购买一群新奶牛.在这个新的奶牛群中,每一个母亲奶牛都生两小奶牛.这些奶牛间的关系可以用二叉树来表示.这些二叉树总共有N个节点(3 <= N < 200).这些二叉 ...
- Log4j实现对Java日志的配置全攻略
1. 配置文件 Log4J配置文件的基本格式如下: #配置根Logger log4j.rootLogger = [ level ] , appenderName1 , appenderName2 , ...
- 由问题引出的fsck命令
博客停了两天,今天打开linux虚拟机,突然间报错了,顿时心中一喜(是吗?),当时看了下错误说明,好像有关于时间的问题(某个时间是未来时间)..然后我就去兴匆匆的修改系统时间,重启...唉,没作用.只 ...
- javascript 键值对
<script type="text/javascript"> var arr = new Array(); arr['cn'] = '中国'; arr['usa'] ...
- [Git]更新远程代码到本地仓库
1. 查看远程仓库 $ git remote -v 2.从远程获取最新代码到本地 $ git fetch origin master 3.比较代码 $ git log -p master.. orig ...
- 如何利用PowerPoint2013制作阶梯流程图?
制作阶梯流程图有哪些窍门呢?下面我们一起来看看吧: ①启动PowerPoint2013,单击菜单栏--插入--形状,选择方角矩形,在图中画出来. ②画好矩形,摆放到合适的位置,如下图所示. ③然后再次 ...
- POJ3617 简单字符串
三分之一的通过率的字符串 题意为,输入一个S串,有一个空串T.对S串有两种操作,一是取出S串的头放入T串的尾,二是取出S串的尾放入T串的尾.要求是要使得T串的字典序最小. 从题意来看是一个很明显的贪心 ...
- 从零开始学Sketch——入门篇-b
如果你是一枚交互设计师或者UI设计师,那么你一定知道Sketch这个强大的矢量设计软件:如果你用过Photoshop,那么在你接触了Sketch之后就能了解到这款产品相对于PS的优点,说不定会跟我一样 ...
- IDE 常用快捷键记录
一:Eclipse (1)删除当前行:Ctrl+D (2)最大化当前编辑窗口:Ctrl+m (3)关闭当前编辑器窗口:Ctrl+F4/Ctrl+w (4)导入依赖包:Ctrl+Shift+o 二:Ne ...
- Cracking the coding interview--Q2.4
Write code to partition a linked list around a value x, such that all nodes less than xcome before a ...