linux后台运行之&和nohup区别,模拟后台守护进程
先来看一下&的使用
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区别,模拟后台守护进程的更多相关文章
- Linux执行shell脚本方式及区别&命令后台运行
Linux执行shell脚本方式及区别&命令后台运行 http://blog.csdn.net/heqiyu34/article/details/19089951/
- Linux后台运行命令,nohup和&的区别
&的意思是在后台运行, 什么意思呢? 意思是说, 当你在执行 ./a.out & 的时候, 即使你用ctrl C, 那么a.out照样运行(因为对SIGINT信号免疫). 但是要注 ...
- Linux 后台运行命令:nohup 和 &
[参开文章]:nohup 与 & 的区别 1. nohup 1.1 基本概念 将程序以忽略挂起信号的方式运行起来: 不可以免疫 Ctrl + C 的 SIGINT 中断信号: 可以免疫 SI ...
- linux后台运行python程序 nohup
nohup python -u test.py > out.log 2>&1 & nohup sh **.sh > /dev/null 2>&1 &am ...
- linux系统添加环境变量,node.js forever 守护进程添加环境变量
1.node.js 守护进程组件 forever 安装 npm install forever -g 安装完成后截图: 2.安装完成后在控制台输入 forever 出现 -bash: forever: ...
- Linux后台运行命令 nohup command > myout.file 2>&1
Linux命令后台运行 转自北国的雨,谢谢:http://www.cnblogs.com/lwm-1988/archive/2011/08/20/2147299.html 有两种方式:1. comma ...
- Linux后台执行命令:&和nohup nohup和&后台运行,进程查看及终止
nohup和&后台运行,进程查看及终止 阅读目录 nohup和&后台运行,进程查看及终止 1.nohup 2.& 3.nohup和&的区别 &:是指在后台运 ...
- linux nohup命令使程序在后台运行的方法
在linux操作系统中从后台一直运行某个程序的方法,就是使用nohup命令了. Unix/Linux下一般比如想让某个程序在后台运行,很多都是使用 & 在程序结尾来让程序自动运行. 比如要运行 ...
- 【Linux学习】nohup后台运行程序以及输出重定向
Linux有两种命令使程序后台运行 第一种:支持后台运行,但是关闭终端的话,程序也会停止 command & 第二种:支持后台运行,关闭终端后,程序也会继续运行 nohup command & ...
随机推荐
- bootstrap学习参考网站
----https://www. evget .com /article /
- Cracking The Coding Interview 1.1
//原文: // // Implement an algorithm to determine if a string has all unique characters. What if you c ...
- 什么是Java优先级队列(Priority Queue)?
PriorityQueue是一个基于优先级堆的无界队列.它的元素是按照自然顺序排序的.在创建元素的时候,我们给它一个一个负责排序的比较器.PriorityQueue不允许null值,因为 它们没有自然 ...
- tp配置
<?php// +----------------------------------------------------------------------// | ThinkPHP [ WE ...
- mybatis column 和property
mybatis map文件中 resultMap中column和sql查询结果对应, property和实体private对应 <resultMap id="VideoYcAppRes ...
- Java基础知识补充
基础知识总结: 学习了一段时间,重新看了孤傲苍狼的博客,对一些知识有了新的理解. unicode: 全球的文字放到计算机里面表示全是0和1,Unicode是统一了全世界国家文字的一种编码方式,用这样的 ...
- Java进程和线程
进程是资源分配和任务调度的基本单位, 进程就是包含上下文切换的程序执行时间总和=CPU加载上下文环境+CPU执行+CPU保存上下文环境,可以理解为时间片段: 进程的颗粒度太大了,将进程分块,按照a,c ...
- Arduino-汉王PM2.5检测模组B1
汉王PM2.5检测模组B1,接入Arduino,使用I2C1602显示屏显示 #include <Arduino.h> #include <Wire.h> #include & ...
- 转--HC05-两个蓝牙模块间的通信
示例蓝牙: 蓝牙A地址:3014:10:271614 蓝牙B地址:2015:2:120758 //============================================= 步骤: 1 ...
- C# 解压
需要ICSharpCode.SharpZipLib.dll 网上有很多 先添加文件引用 再添加引用 using ICSharpCode.SharpZipLib.Zip; #region 解压 /// ...