先来看一下&的使用

root@BP:~# cat test.sh
#!/bin/bash
while true
do
echo "linux">/dev/null
done
root@BP:~# ./test.sh & #&后台运行
[1] 4599
root@BP:~# ps #test.sh运行中
PID TTY TIME CMD
4555 pts/1 00:00:00 bash
4599 pts/1 00:00:00 test.sh
4600 pts/1 00:00:00 ps
root@BP:~# exit
root@BP:~# ps #终端退出后程序也跟着退出了
PID TTY TIME CMD
4611 pts/2 00:00:00 bash
4617 pts/2 00:00:00 ps
root@BP:~#

再来看一下nohup,使用该命令会在当前目录下产生nohup.out文件,该文件类似日志文件,程序的输出直接记录到该文件,不过我这里直接/dev/null了,所以那个文件会是空的

根据下面的步骤执行,就不会出现nohup随着终端关闭而停止程序的情况了,特别是退出方式那里特别需要注意(这个是别人说的,经过我实验nohup ./test.sh &回车后直接点击关闭窗口也没有什么影响)

就我个人来说,我就是老是以为可以直接用ps查看到这个进程,导致我在这里卡了很久而已

root@BP:~# cat test.sh
#!/bin/bash
while true
do
echo "linux">/dev/null
done
root@BP:~# ps -aux|grep test.sh
root 5093 0.0 0.0 12720 932 pts/2 S+ 09:10 0:00 grep test.sh
root@BP:~# nohup ./test.sh &
[1] 5094
root@BP:~# nohup: ignoring input and appending output to 'nohup.out' #需要回车一下,继续执行下面命令 root@BP:~# ps #我一直在这里查看test.sh有没有运行其实是错误的
PID TTY TIME CMD
4716 pts/2 00:00:00 bash
5094 pts/2 00:00:21 test.sh
5096 pts/2 00:00:00 ps
root@BP:~# ps -aux|grep test.sh #应该这样子看才对
root 5094 98.8 0.0 11156 2716 pts/2 R 09:10 1:22 /bin/bash ./test.sh
root 5102 0.0 0.0 12720 940 pts/2 S+ 09:12 0:00 grep test.sh
root@BP:~# exit #需要以这种方式退出终端
#退出终端后,再重新开一个终端执行下面命令
root@BP:~# ps
PID TTY TIME CMD
5132 pts/1 00:00:00 bash
5138 pts/1 00:00:00 ps
root@BP:~# ps -aux|grep test.sh #可以看到程序并没有停止,注意5094是pid
root 5094 99.0 0.0 11156 2716 ? R 09:10 5:50 /bin/bash ./test.sh
root 5142 0.0 0.0 12720 944 pts/1 S+ 09:16 0:00 grep test.sh
root@BP:~# 扩展一下:怎么杀死这个后台进程
root@BP:~# kill -9 5094
root@BP:~# ps -aux|grep test.sh
root 5163 0.0 0.0 11116 936 pts/1 D+ 09:20 0:00 grep test.sh

linux后台运行之&和nohup区别,模拟后台守护进程的更多相关文章

  1. Linux执行shell脚本方式及区别&命令后台运行

    Linux执行shell脚本方式及区别&命令后台运行 http://blog.csdn.net/heqiyu34/article/details/19089951/

  2. Linux后台运行命令,nohup和&的区别

    &的意思是在后台运行, 什么意思呢?  意思是说, 当你在执行 ./a.out & 的时候, 即使你用ctrl C,  那么a.out照样运行(因为对SIGINT信号免疫). 但是要注 ...

  3. Linux 后台运行命令:nohup 和 &

    [参开文章]:nohup 与 & 的区别 1. nohup 1.1 基本概念 将程序以忽略挂起信号的方式运行起来: 不可以免疫 Ctrl + C  的 SIGINT 中断信号: 可以免疫 SI ...

  4. linux后台运行python程序 nohup

    nohup python -u test.py > out.log 2>&1 & nohup sh **.sh > /dev/null 2>&1 &am ...

  5. linux系统添加环境变量,node.js forever 守护进程添加环境变量

    1.node.js 守护进程组件 forever 安装 npm install forever -g 安装完成后截图: 2.安装完成后在控制台输入 forever 出现 -bash: forever: ...

  6. Linux后台运行命令 nohup command > myout.file 2>&1

    Linux命令后台运行 转自北国的雨,谢谢:http://www.cnblogs.com/lwm-1988/archive/2011/08/20/2147299.html 有两种方式:1. comma ...

  7. Linux后台执行命令:&和nohup nohup和&后台运行,进程查看及终止

    nohup和&后台运行,进程查看及终止   阅读目录 nohup和&后台运行,进程查看及终止 1.nohup 2.& 3.nohup和&的区别 &:是指在后台运 ...

  8. linux nohup命令使程序在后台运行的方法

    在linux操作系统中从后台一直运行某个程序的方法,就是使用nohup命令了. Unix/Linux下一般比如想让某个程序在后台运行,很多都是使用 & 在程序结尾来让程序自动运行. 比如要运行 ...

  9. 【Linux学习】nohup后台运行程序以及输出重定向

    Linux有两种命令使程序后台运行 第一种:支持后台运行,但是关闭终端的话,程序也会停止 command & 第二种:支持后台运行,关闭终端后,程序也会继续运行 nohup command & ...

随机推荐

  1. 网页的MVC模式简介

    #! /usr/bin/env python3 # -*- coding:utf-8 -*- #MVC:Model-View-Controller 模型-视图-控制器 #Python处理URL的函数就 ...

  2. lxml简单用法 解析网页

    import requests s=requests.Session() re=s.get(lgurl,headers=headers)  #此处s可以直接换成requests the_page=re ...

  3. 八. Python基础(8)--函数

    八. Python基础(8)--函数 1 ● 函数返回布尔值 注意, 自定义的函数也可以是用来作逻辑判断的, 例如内置的startswith()等函数. def check_len(x):     ' ...

  4. Cracking The Coding Interview 3.5

    //Implement a MyQueue class which implements a queue using two stacks. #include <iostream> #in ...

  5. Chrome浏览器录屏扩展插件

    Chrome浏览器录屏扩展插件,可以录制网页操作或者桌面操作.生成MP4 Loom https://chrome.google.com/webstore/detail/loom-video-recor ...

  6. SUSE_LINUX 11 SP3 安装 IBM MQ 7.5

    0.环境介绍 mq7.5 suse linux 11 1. 上传安装包 上传安装包到 softWare/CI79IML.tar.gz 2. 安装证书 sh ./mqlicense.sh 输入 1 同意 ...

  7. SharePoint online Multilingual support - Development(2)

    博客地址:http://blog.csdn.net/FoxDave 上一节讲了如何通过Code的方式设置Site和List级别的国际化,本节介绍一下如何设置Content type和Site co ...

  8. SpringMVC学习四(@ModelMap @RequestBody等等的说明)

    参考如下 http://www.cnblogs.com/HD/p/4107674.html http://www.cnblogs.com/qiankun-site/p/5774325.html @re ...

  9. for循环遍历改用map函数

    # for url in urls:# url = response.urljoin(url)# print(url)urls = map(lambda url:response.urljoin(ur ...

  10. java学习笔记17(Calendarl类)

    Calendar类:(日历) 用法:Calendar是一个抽象类:不能实例化(不能new),使用时通过子类完成实现,不过这个类不需要创建子类对象,而是通过静态方法直接获取: 获取对象方法:getIns ...