打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
linux命令后台运行(& nohup setsid subshell bg fg jobs)
         1. command & : 后台运行,你关掉终端会停止运行。

2. nohup command & : 后台运行,你关掉终端也会继续运行。

3. setsid command & : 后台运行,你关掉终端也会继续运行。

4. (command &) : 后台运行,你关掉终端也会继续运行。


一、bg、fg、jobs

Linux提供了fg和bg命令,让你轻松调度正在运行的任务。假设你发现前台运行的一个程序需要很长的时间,但是需要干其他的事情,你就可以用ctrl-z,挂起这个程序,然后可以看到系统提示,然后我们可以把程序调度到后台执行。
tail -f main.cpp
[1]+  Stopped                 tail -f main.cpp

把程序调度到后台执行:
bg 1
查看正在运行的程序:
jobs -l
[1]+ 17963 Running                 tail -f main.cpp &
把程序调度回前台执行:
fg 1


二、&(当前shell为父进程,shell退出后子进程立即退出)

显示当前shell的PID:echo $$

tail -f main.cpp &
jobs -l
[1]+ 17963 Running                 tail -f main.cpp &


三、nohup &(当前shell为父进程,shell退出后忽略hangup信号,并设置父进程为init)

nohup tail -f main.cpp & #一般会进行输出重定向,否则产生nohup.out文件。

jobs -l
[1]+ 17963 Running                 tail -f main.cpp &


四、setsid(init直接为父进程)

ps -ef | grep tail

root     22520     1  0 22:53 ?        00:00:00 tail -f main.cpp
root     22522 22495  0 22:53 pts/0    00:00:00 grep tail


五、(command &)(进程在subshell中执行,等价于setsid)


ps -ef | grep tail
root     22520     1  0 22:53 ?        00:00:00 tail -f main.cpp
root     22522 22495  0 22:53 pts/0    00:00:00 grep tail


六、python脚本或者shell脚本启动的后台进程

1、ctrl+c 脚本被杀死 第一个tail没被杀死 第二个tail被杀死

2、kill  脚本被杀死 第一个和第二个tail都没有被杀死


#!/bin/bash

tail -f main.cpp &

tail -f main.cpp


#!/usr/bin/bash

import os

os.system('tail -f main.cpp &')

os.system('tail -f main.cpp')

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Linux 进程后台运行的几种方式(screen)
这些进程的后台可靠运行命令你都知道了吗
UNIX进程组,会话和作业控制
如何让进程在Linux后台运行
大数据开发-linux后台运行,关闭,查看后台任务
让我带你玩转”Linux“
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服