ubuntu python 脚本(ubuntu自带python吗)

今天给各位分享ubuntu python 脚本的知识,其中也会对ubuntu自带python吗进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

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

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

Ubuntu18.04 部署Python脚本开机自启,稳定解决方案

Ubuntu在16.04版本后,系统管理转向使用systemd,直接通过软链接或创建systemd文件来启动服务变得不太方便,尤其是对于日志输出和守护进程的管理。为了解决这些问题,引入PM2成为更高效、直观的选择。

PM2是一个生产级进程管理器,专门用于管理后台进程,提供简单、方便的命令行操作,如重启崩溃程序、查看日志和资源使用情况。要开始使用PM2,首先需要安装Node.js和npm,尽管PM2是Node.js环境下的工具,但同样适用于Python脚本的启动。

启动Python脚本时,使用命令`pm2 start文件路径`。重要的是在脚本内指定解释器路径,或使用打包后的文件。例如,这里使用了pyinstaller打包的文件。

为了监控Python脚本的运行情况,可以通过`pm2 logs应用名称`查看进程日志。另外,`pm2 logs-d`命令可以显示所有应用的日志信息。使用`pm2 logs-f应用名称`可以实时查看日志。

要查看进程描述信息,可以使用`pm2 info应用名称`命令,它会显示日志的存储路径、解释器、运行时间、脚本路径等详细信息。

遇到问题需要重启或停止进程时,使用`pm2 restart应用名称`或`pm2 stop应用名称`命令即可。若需彻底删除进程,执行`pm2 delete应用名称`。

要实现开机自启,安装的PM2默认不会随系统启动。通过创建systemd文件和创建软链接,可让PM2在Ubuntu18.04上实现开机自启。具体步骤包括保存转存文件,系统自动添加软链接,对于不同版本的Ubuntu,可能需要按照系统提示执行相应的命令来完成自启服务的创建。

总之,通过引入PM2,不仅简化了Python脚本的开机自启、守护进程管理和日志监控,还提供了更高效、直观的流程管理体验。这一解决方案在实际应用中证明了其稳定性和实用性。

如何在ubuntu的桌面上双击执行py脚本

工作中需要经常写python脚本,平时执行的时候都是在命令终端窗口执行的脚本,那么如何在桌面双击文件执行py脚本?让我们一起来看看。

具体如下:

1.首先,在桌面我们创建一个test的py脚本,如图。

2.其次打开右边箭头所指的文件管理窗口。

3.然后在上方菜单栏里点击编辑后,点击框出的--首选项。

4.接着,在弹出窗口中点击框出的行为。

5.然后在可执行文本文件下选择,推荐选择框出的第一个或者第三个,每次都询问。

6.关闭弹窗后,在桌面上鼠标单击右键后,点击打开终端。

7.然后我们通过cd Desktop这一选项进入桌面。

8.然后,通过图中框出的chmod x文件名给py脚本赋予可执行权限。

9.做完以上后,双击text.py就可以看见弹框,单击在终端中运行就可以直接运行啦~

以上就是完整的在ubuntu的桌面上直接执行py的全部过程啦,希望对大家有所帮助。

阅读剩余
THE END