转载nohup

https://www.cnblogs.com/evilliu/p/7682444.html
https://blog.csdn.net/maihilton/article/details/82781897
运行python脚本后台执行
在Linux中,可以使用nohup将脚本放置后台运行,如下:

nohup python myscript.py params1 > nohup.out 2>&1 &
1
但直接使用上面代码,无法在程序运行过程中查看Python中的print “computing” 输出结果,比如在每次循环中使用print语句等。原因是python的输出有缓冲,导致nohup.out不能够马上看到输出。

解决方法:

使用-u参数,使得python不启用缓冲。
修改命令如下:

nohup python -u myscript.py params1 > nohup.out 2>&1 &
1
2
这样就可以同步看到输出结果了。

学习是一种态度,坚持是质变的利器!

yinhaibo@yinhaibo-OptiPlex-9020:~$ kill 11655
yinhaibo@yinhaibo-OptiPlex-9020:~$ vim rest.sh
yinhaibo@yinhaibo-OptiPlex-9020:~$ nohup sh rest.sh params1 > nohup.out 2>&1 &
[1] 16093
yinhaibo@yinhaibo-OptiPlex-9020:~$ jobs
[1]+  Running                 nohup sh rest.sh params1 > nohup.out 2>&1 &
yinhaibo@yinhaibo-OptiPlex-9020:~$ ps aux|grep res
message+   889  0.0  0.0  44464  4816 ?        Ss   12月18   0:04 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
nobody    1135  0.0  0.0  59932  3712 ?        S    12月18   0:01 /usr/sbin/dnsmasq --no-resolv --keep-in-foreground --no-hosts --bind-interfaces --pid-file=/var/run/NetworkManager/dnsmasq.pid --listen-address=127.0.1.1 --cache-size=0 --conf-file=/dev/null --proxy-dnssec --enable-dbus=org.freedesktop.NetworkManager.dnsmasq --conf-dir=/etc/NetworkManager/dnsmasq.d
yinhaibo  1635  0.0  0.0  46968  7140 ?        Ss   12月18   1:11 dbus-daemon --fork --session --address=unix:abstract=/tmp/dbus-iOLbQilmGD
yinhaibo  1698  0.0  0.0  43276  3604 ?        Rs   12月18   0:24 /usr/bin/dbus-daemon --fork --print-pid 5 --print-address 7 --config-file /usr/share/fcitx/dbus/daemon.conf
yinhaibo  1781  0.0  0.0  43028  3544 ?        S    12月18   0:01 /usr/bin/dbus-daemon --config-file=/etc/at-spi2/accessibility.conf --nofork --print-address 3
yinhaibo  2137  0.0  0.1 697636 10236 ?        Sl   12月18   0:00 /usr/lib/evolution/evolution-addressbook-factory
yinhaibo  2158  0.0  0.1 773132 10748 ?        Sl   12月18   0:00 /usr/lib/evolution/evolution-addressbook-factory-subprocess --factory local --bus-name org.gnome.evolution.dataserver.Subprocess.Backend.AddressBookx2137x2 --own-path /org/gnome/evolution/dataserver/Subprocess/Backend/AddressBook/2137/2
yinhaibo 16093  0.0  0.0   4504   844 pts/22   S    20:05   0:00 sh rest.sh params1
yinhaibo 16107  0.0  0.0  21292   972 pts/22   S+   20:06   0:00 grep --color=auto res
yinhaibo@yinhaibo-OptiPlex-9020:~$ ps aux|grep rest
yinhaibo 16093  0.0  0.0   4504   844 pts/22   S    20:05   0:00 sh rest.sh params1
yinhaibo 16109  0.0  0.0  21292   924 pts/22   S+   20:06   0:00 grep --color=auto rest
yinhaibo@yinhaibo-OptiPlex-9020:~$ jobs
[1]+  Running                 nohup sh rest.sh params1 > nohup.out 2>&1 &
yinhaibo@yinhaibo-OptiPlex-9020:~$ fg
nohup sh rest.sh params1 > nohup.out 2>&1
^C
yinhaibo@yinhaibo-OptiPlex-9020:~$ fg
bash: fg: current: no such job
yinhaibo@yinhaibo-OptiPlex-9020:~$ jobs
yinhaibo@yinhaibo-OptiPlex-9020:~$ ps aux|grep rest
yinhaibo 16122  0.0  0.0  21292   972 pts/22   S+   20:07   0:00 grep --color=auto rest
yinhaibo@yinhaibo-OptiPlex-9020:~$ nohup sh rest.sh params1 > nohup.out 2>&1 &
[1] 16135
yinhaibo@yinhaibo-OptiPlex-9020:~$ 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值