导航:首页 > 程序命令 > nohup命令运行python

nohup命令运行python

发布时间:2022-05-31 07:01:56

linux 系统中 nohup命令如何使用

linux的nohup命令的用法。

在应用Unix/Linux时,我们一般想让某个程序在后台运行,于是我们将常会
用 & 在程序结尾来让程序自动运行。比如我们要运行mysql在后台: /usr/local/mysql/bin/mysqld_safe
–user=mysql
&。可是有很多程序并不想mysqld一样,这样我们就需要nohup命令,怎样使用nohup命令呢?这里讲解nohup命令的一些用法。
nohup /root/start.sh &
在shell中回车后提示:
[~]$ appending output to nohup.out
原程序的的标准输出被自动改向到当前目录下的nohup.out文件,起到了log的作用。
但是有时候在这一步会有问题,当把终端关闭后,进程会自动被关闭,察看nohup.out可以看到在关闭终端瞬间服务自动关闭。
咨询红旗Linux工程师后,他也不得其解,在我的终端上执行后,他启动的进程竟然在关闭终端后依然运行。
在第二遍给我演示时,我才发现我和他操作终端时的一个细节不同:他是在当shell中提示了nohup成功后还需要按终端上键盘任意键退回到
shell输入命令窗口,然后通过在shell中输入exit来退出终端;而我是每次在nohup执行成功后直接点关闭程序按钮关闭终端.。所以这时候会
断掉该命令所对应的session,导致nohup对应的进程被通知需要一起shutdown。
这个细节有人和我一样没注意到,所以在这儿记录一下了。
附:nohup命令参考
nohup 命令
用途:不挂断地运行命令。
语法:nohup Command [ Arg … ] [& ]
描述:nohup 命令运行由 Command 参数和任何相关的 Arg 参数指定的命令,忽略所有挂断(SIGHUP)信号。在注销后使用
nohup 命令运行后台中的程序。要运行后台中的 nohup 命令,添加 & ( 表示”and”的符号)到命令的尾部。
无论是否将 nohup 命令的输出重定向到终端,输出都将附加到当前目录的 nohup.out 文件中。如果当前目录的 nohup.out
文件不可写,输出重定向到 $HOME/nohup.out 文件中。如果没有文件能创建或打开以用于追加,那么 Command
参数指定的命令不可调用。如果标准错误是一个终端,那么把指定的命令写给标准错误的所有输出作为标准输出重定向到相同的文件描述符。
退出状态:该命令返回下列出口值:
126 可以查找但不能调用 Command 参数指定的命令。
127 nohup 命令发生错误或不能查找由 Command 参数指定的命令。
否则,nohup 命令的退出状态是 Command 参数指定命令的退出状态。
nohup命令及其输出文件
nohup命令:如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,那么可以使用nohup命令。该命令可以在你退出帐户/关闭终端之后继续运行相应的进程。nohup就是不挂起的意思( n ohang up)。
该命令的一般形式为:nohup command &
使用nohup命令提交作业
如果使用nohup命令提交作业,那么在缺省情况下该作业的所有输出都被重定向到一个名为nohup.out的文件中,除非另外指定了输出文件:
nohup command > myout.file 2>&1 &
在上面的例子中,输出被重定向到myout.file文件中。
使用 jobs 查看任务。
使用 fg %n关闭。

❷ 在虚拟主机中安装了python程序,如何使它在服务器上自动运行

nohup 和 screen 都可以解决:

nohup python script.py &

或者

screen python script.py

❸ 如何远程登录Linux机器并运行Python程序

新手对于没有图形界面的linux远程登录及其操作都充满畏惧。这里介绍一个简单的软件。叫作BitViz。简称BV for short。

一、软件安装
这里使用Putty的一个client软件叫作Bv SshClient. 你可以在putty的官网上找到其链接。

Bitvise Tunnelier

Tunnelier is an SSH and SFTP client for Windows. It is developed and supported professionally by Bitvise. Tunnelier is robust, easy to install, easy to use, and supports all features supported by PuTTY, as well as the following:

graphical SFTP file transfer; 图形界面最喜欢
single-click Remote Desktop tunneling;
auto-reconnecting capability;
dynamic port forwarding through an integrated proxy;
an FTP-to-SFTP protocol bridge. 方便的上传下载和删除操作。
Tunnelier is free for personal use, as well as for indivial commercial use inside organizations. You can download Tunnelier here.
二、熟悉窗口
下面一个例子,是找到python软件安装位置的演示:
1. 首先,我进入到root

步骤一,进入root

2. 输入/home

步骤二,进入home

3. 点击其中子文件夹,即可找到,方便了。

步骤三,找到python文件夹

三、easy_install python library
Example Of python-setuptools Being Installed:
[root@server ~]# yum install python-setuptools

Yum Command To Install python-setuptools-devel:
[root@server ~]#yum install python-setuptools-devel

在linux 下: 使用方法非常简单,在命令行输入“easy_install 参数”即可。
这比我想象的要方便很多!在windows里,我要cmd-cd & easy_install flickrapi
在ssh的命令窗口,只需输入 easy_install flickrapi

如下图:

easy_install flickrapi

四、run python script as a background process in linux

So, you have a server to which you connect remotely, upload a python script and want to run it and logout from the server keeping the program running. If you frequently work with spiders, you surely want to do it. But how to do it? For example if your script's name is script.py, then the command is:

[root@server ~]# nohup python script.py &

And sometimes you may be interested to see the output is that being generated. Then you should view the nohup.out file! This command can be useful:

[root@server ~]# tail -f nohup.out

❹ nohup命令详解是什么

nohup 命令运行由 Command 参数和任何相关的 Arg 参数指定的命令,忽略所有挂断(SIGHUP)信号。在注销后使用 nohup 命令运行后台中的程序。要运行后台中的 nohup 命令,添加 & ( 表示“and”的符号)到命令的尾部。

如果不将 nohup 命令的输出重定向,输出将附加到当前目录的 nohup.out 文件中。如果当前目录的 nohup.out 文件不可写,输出重定向到 $HOME/nohup.out 文件中。

运用须知

如果没有文件能创建或打开以用于追加,那么 Command 参数指定的命令不可调用。如果标准错误是一个终端,那么把指定的命令写给标准错误的所有输出作为标准输出重定向到相同的文件描述符。

如果正在运行一个进程,而且觉得在退出账户时该进程还不会结束,那么可以使用 nohup 命令。该命令可以在你退出帐户/关闭终端之后继续运行相应的进程。nohup 就是不挂断的意思( no hang up)。

❺ nohup命令的用法

nohup命令的用法在应用Unix/linux时,我们一般想让某个程序在后台运行,于是我们将常会用
&
在程序结尾来让程序自动运行。比如我们要运行mysql在后台:
/usr/local/mysql/bin/mysqld_safe
–user=mysql
&。可是有很多程序并不想mysqld一样,这样我们就需要nohup命令,怎样使用nohup命令呢?这里讲解nohup命令的一些用法。nohup
/root/start.sh
&在shell中回车后提示:[~]$
appending
output
to
nohup.out原程序的的标准输出被自动改向到当前目录下的nohup.out文件,起到了log的作用。但是有时候在这一步会有问题,当把终端关闭后,进程会自动被关闭,察看nohup.out可以看到在关闭终端瞬间服务自动关闭。咨询红旗linux工程师后,他也不得其解,在我的终端上执行后,他启动的进程竟然在关闭终端后依然运行。在第二遍给我演示时,我才发现我和他操作终端时的一个细节不同:他是在当shell中提示了nohup成功后还需要按终端上键盘任意键退回到shell输入命令窗口,然后通过在shell中输入exit来退出终端;而我是每次在nohup执行成功后直接点关闭程序按钮关闭终端.。所以这时候会断掉该命令所对应的session,导致nohup对应的进程被通知需要一起shutdown。这个细节有人和我一样没注意到,所以在这儿记录一下了。附:nohup命令参考nohup
命令用途:不挂断地运行命令。语法:nohup
Command
[
Arg
]
[&
]描述:nohup
命令运行由
Command
参数和任何相关的
Arg
参数指定的命令,忽略所有挂断(SIGHUP)信号。在注销后使用
nohup
命令运行后台中的程序。要运行后台中的
nohup
命令,添加
&

表示and的符号)到命令的尾部。无论是否将
nohup
命令的输出重定向到终端,输出都将附加到当前目录的
nohup.out
文件中。如果当前目录的
nohup.out
文件不可写,输出重定向到
$HOME/nohup.out
文件中。如果没有文件能创建或打开以用于追加,那么
Command
参数指定的命令不可调用。如果标准错误是一个终端,那么把指定的命令写给标准错误的所有输出作为标准输出重定向到相同的文件描述符。退出状态:该命令返回下列出口值:126
可以查找但不能调用
Command
参数指定的命令。127
nohup
命令发生错误或不能查找由
Command
参数指定的命令。否则,nohup
命令的退出状态是
Command
参数指定命令的退出状态。nohup命令及其输出文件nohup命令:如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,那么可以使用nohup命令。该命令可以在你退出帐户/关闭终端之后继续运行相应的进程。nohup就是不挂起的意思(
n
ohang
up)。该命令的一般形式为:nohup
command
&使用nohup命令提交作业如果使用nohup命令提交作业,那么在缺省情况下该作业的所有输出都被重定向到一个名为nohup.out的文件中,除非另外指定了输出文件:nohup
command
>
myout.file
2>&1
&在上面的例子中,输出被重定向到myout.file文件中。

❻ ubuntu python怎么作为守护进程一直运行

测试程序
先写一个测试程序,用于输出日志和打印到控制台。
#-*- coding: utf-8 -*-
import logging
import time
from logging.handlers import RotatingFileHandler

def func():
init_log()
while True:
print "output to the console"
logging.debug("output the debug log")
logging.info("output the info log")
time.sleep(3);

def init_log():
logging.getLogger().setLevel(logging.DEBUG)
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')
console.setFormatter(formatter)
logging.getLogger().addHandler(console)

# add log ratate
Rthandler = RotatingFileHandler("backend_run.log", maxBytes=10 * 1024 * 1024, backupCount=100,
encoding="gbk")
Rthandler.setLevel(logging.INFO)
# formatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')
Rthandler.setFormatter(formatter)
logging.getLogger().addHandler(Rthandler)

if __name__ == '__main__':
func()

后台启动Python脚本
可以使用下面的命令来启动上面的脚本,让Python在后台运行。
nohup python -u main.py > test.out 2>&1 &

来解释一下这几个命令的参数。这一段来自
其中 0、1、2分别代表如下含义:
0 – stdin (standard input)
1 – stdout (standard output)
2 – stderr (standard error)
nohup python -u main.py > test.out 2>&1 &
nohup+最后面的& 是让命令在后台执行
>out.log 是将信息输出到out.log日志中
2>&1 是将标准错误信息转变成标准输出,这样就可以将错误信息输出到out.log 日志里面来。

运行命令后,会返回一个pid。像下面这样:
[1] 9208

后续可以学习Hadoop它们,把pid存起来,到时候stop的时候就把它杀掉。
跟踪输出文件变化
为了验证脚本可以在后台继续运行,我们退出当前会话。然后重新连接一个Session,然后输入下面的命令来跟踪文件的输出:
tail -f test.out

输出内容如下:
output to the console
2017-03-21 20:15:02,632 main.py[line:11] DEBUG output the debug log
2017-03-21 20:15:02,632 main.py[line:12] INFO output the info log
output to the console
2017-03-21 20:15:05,635 main.py[line:11] DEBUG output the debug log
2017-03-21 20:15:05,636 main.py[line:12] INFO output the info log
output to the console
2017-03-21 20:15:08,637 main.py[line:11] DEBUG output the debug log
2017-03-21 20:15:08,638 main.py[line:12] INFO output the info log
output to the console
2017-03-21 20:15:11,640 main.py[line:11] DEBUG output the debug log
2017-03-21 20:15:11,642 main.py[line:12] INFO output the info log
output to the console
2017-03-21 20:15:14,647 main.py[line:11] DEBUG output the debug log
2017-03-21 20:15:14,647 main.py[line:12] INFO output the info log
output to the console
2017-03-21 20:15:17,653 main.py[line:11] DEBUG output the debug log
2017-03-21 20:15:17,654 main.py[line:12] INFO output the info log
output to the console
2017-03-21 20:15:20,655 main.py[line:11] DEBUG output the debug log
2017-03-21 20:15:20,656 main.py[line:12] INFO output the info log
output to the console
2017-03-21 20:15:23,661 main.py[line:11] DEBUG output the debug log
2017-03-21 20:15:23,661 main.py[line:12] INFO output the info log
output to the console
2017-03-21 20:15:26,665 main.py[line:11] DEBUG output the debug log
2017-03-21 20:15:26,666 main.py[line:12] INFO output the info log
output to the console
2017-03-21 20:15:29,670 main.py[line:11] DEBUG output the debug log
2017-03-21 20:15:29,671 main.py[line:12] INFO output the info log

说明我们的脚本确实在后台持续运行。
结束程序
可以直接通过之前的那个pid杀掉脚本,或者可以通过下面的命令查找pid。
ps -ef | grep python

输出的内容如下:
root 1659 1 0 17:40 ? 00:00:00 /usr/bin/python /usr/lib/python2.6/site-packages/ambari_agent/AmbariAgent.py start
root 1921 1659 0 17:40 ? 00:00:48 /usr/bin/python /usr/lib/python2.6/site-packages/ambari_agent/main.py start
user 8866 8187 0 20:03 ? 00:00:06 /usr/bin/python3 /usr/bin/update-manager --no-update --no-focus-on-map
root 9208 8132 0 20:12 pts/16 00:00:00 python -u main.py
root 9358 8132 0 20:17 pts/16 00:00:00 grep --color=auto python

可以看到我们的pid是9208,调用kill杀掉就可以了。
kill -9 9208

编写启动及停止脚本
启动脚本
#!/bin/sh

pid=`ps -ef|grep "python -u main.py"| grep -v "grep"|awk '{print $2}'`

if [ "$pid" != "" ]
then
echo "main.py already run, stop it first"
kill -9 ${pid}
fi

echo "starting now..."

nohup python -u main.py > test.out 2>&1 &

pid=`ps -ef|grep "python -u main.py"| grep -v "grep"|awk '{print $2}'`

echo ${pid} > pid.out
echo "main.py started at pid: "${pid}

停止脚本
#!/bin/sh

pid=`ps -ef|grep "python -u main.py"| grep -v "grep"|awk '{print $2}'`

if [ "$pid" != "" ]
then
kill -9 ${pid}
echo "stop main.py complete"
else
echo "main.py is not run, there's no need to stop it"
fi

稍后我会把实例代码上传到资料共享里面。

❼ linux 怎么一直运行python搜索脚本

有两种方式:
1、直接使用python xxxx.py执行。其中python可以写成python的绝对路径。使用which python进行查询。
2、在文件的头部(第一行)写上#!/usr/bin/python2.7,这个地方使用python的绝对路径,就是上面用which python查询来的结果。然后在外面就可以使用./xxx.py执行了。

因为在linux中,python啊shell这些程序都是普通的文本格式,都需要一种程序去解释执行它。要么调用的时候指定,要么在文件头指定。更多linux知识可以看下《linux就该这么学》

❽ 直接python程序运行没有问题,nohup运行就出错了,该怎么解决

nohup 和 screen 都可以解决: nohup python script.py & 或者 screen python script.py

php 执行python脚本的时候 已经安装python模块能正常使用么

一般是环境变量问题,你的执行一个sh的set命令看看结果吧。
解决办法,在sh文件里面设置好环境变量再调用python脚本。

❿ 如何让 Python 代码常驻在服务器进程中

一、nohup

nohup,即 no hangup,nohup 的用途就是让提交的命令忽略 hangup 信号,从而使我们的进程避免中途被中断。它可以让 python 脚本和服务器连接端的 session 分离,以达到退出后依旧执行:

$ chmod +x /your_path/yourscript.py # 先设置可执行权限$ nohup python /your_path/yourscript.py# 切记退出的 Terminal 的时候,不要 ctrl+c 退出,而是直接关闭,不然 Nohup 就被你关闭了。

如何关闭这个一直执行的进程呢:

# 找到对应的进程 PID
$ ps -ef | grep python

# 返回内容如:
user 2430 1 0 Jul03 ? 00:00:01 /usr/bin/python -tt /usr/sbin/yum-updatesd

# kill 掉该进程即可:
$ kill -9 2430

PS:nohup 在服务器重启之后就失效了,所以并不完美。

二、将命令写入 Linux 启动脚本

Linux 在启动的时候会执行 /etc/rc.local 里面的脚本,所以只要在这里添加执行命令就可以:

$ vim /etc/rc.local

# 如果是 Centos 添加以下内容:
/your_path/python3.4 /your_path/yourscript.py

# 以上内容需要添加在 exit 命令前,而且由于在执行 rc.local 脚本时,PATH 环境变量未全部初始化,因此命令需要使用绝对路径。

PS:这种方式的缺点是如果脚本挂了,那么不会自动重新启动。

三、使用 Supervisor 进程管理工具

详见这篇文章:使用 Supervisor 管理服务器后台进程,在服务器重启和脚本出错后,可以完美重启,推荐。

阅读全文

与nohup命令运行python相关的资料

热点内容
bpmx3源码 浏览:121
通信类单片机 浏览:815
加密植发的区别 浏览:536
程序员跑需求 浏览:224
s7服务器怎么设置 浏览:343
2k17连接不上服务器怎么办 浏览:493
人力资源系统开源源码 浏览:687
河北视频加密有哪些 浏览:651
桌面两个微信怎么都加密码 浏览:278
长沙单身程序员 浏览:886
服务器下载异常是因为什么 浏览:705
java防刷 浏览:844
3dmax取消当前命令 浏览:361
显示当前模式下所有可执行的命令 浏览:760
为什么程序员拿了股份还要高薪 浏览:949
电脑运行命令里的记录能删吗 浏览:699
linuxwss 浏览:850
一个软件需要登录服务器地址 浏览:925
哪里有解压程序 浏览:301
java静态方法内存 浏览:547