⑴ 如何将我的php脚本以守护进程的方式一直运行
写好php脚本。建议定期检测内存占用,核心逻辑就不写了。这个跟业务有关。
if(memory_get_usage()>100*1024*1024){
exit(0);//大于100M内存退出程序,防止内存泄漏被系统杀死导致任务终端
}
假设该php文件的路径为/root/run.php
打开终端
setsid php /root/run.php > /dev/null &
编辑进程监控脚本,当进程不存在时,自动重启 /root/monitor.sh
#!/bin/bash
alive=`ps aux|grep root\/run|grep -v grep|wc -l`
if [ $alive -eq 0]
then
php /root/run.php > /dev/null &
fi
添加计划任务(每分钟检测一次)
crontab -e
* * * * * /root/monitor.sh > /dev/null &
⑵ 用按键精灵做了个脚本 但被游戏检测进程检测出来了 进程如何不被检测
方法/步骤
1.下载安装天狼进程隐藏工具,下载地址问网络。电脑上提前安好按键精灵。
⑶ 如何使shell脚本成为守护进程
1,if条件语句里面表达式要与左右[ ] 用空格隔开,否则运行时会报错
2,用表达式给变量负值时要将表达式放在括号()里面,并在前面加符号"$",应该还有其他方法 目前只会这种,在参考那篇博客用单引号运行无法通过。
3,ps aux | grep $PROGRAM | grep -v grep | wc -l 此表达式检测运行进程数量
4,ps aux | grep $PROGRAM | grep T | grep -v grep | wc -l 此表达式检测进程运行状态,0:运行 1:停止 但进程依然存在 可以通过命令 kill -SIGSTOP pid 进行测试
第一个条件判断目标进程运行数目 数目小于1即为0 则运行目标进程
第二条件判断目标进程是否处于停止状态 处于停止状态则kill掉 重新运行该进程
应用程序编译完及脚本编写好后,在/etc/rc.local 文件中添加执行语句 /usr/mytest/mytest.sh & 重启后就能自动加载守护进程了。
⑷ win10 64位如何强制保护我想要保护的进程,防止它被结束
在你的脚本中加入:strComputer = "mandline),"C:\PROmandline),"C:\TEST.VBS") then objProcess.Create("wscript.exe C:\TEST.VBS") end if Loop其中C:\TEST.VBS,就是你的主脚本路径,根据实际情况该写,但是C:\TEST.VBS需要大写 直接运行你的主脚本C:\TEST.VBS即可,不需要你手动运行保护脚本
⑸ 如何将我的php脚本以守护进程的方式一直运行
写好php脚本。建议定期检测内存占用,核心逻辑就不写了。这个跟业务有关。
if(memory_get_usage()>100*1024*1024){
exit(0);//大于100M内存退出程序,防止内存泄漏被系统杀死导致任务终端
}
假设该php文件的路径为/root/run.php
打开终端
setsid php /root/run.php > /dev/null &
编辑进程监控脚本,当进程不存在时,自动重启 /root/monitor.sh
#!/bin/bash
alive=`ps aux|grep root\/run|grep -v grep|wc -l`
if [ $alive -eq 0]
then
php /root/run.php > /dev/null &
fi
⑹ 如何给脚本写一个守护进程
在我们日常运维中,写脚本监控一个进程是比较常见的操作,比如我要监控mysql进程是否消失,如果消失就重启mysql,用下面这段代码就可以实现:
#!/bin/sh
Date=` date '+%c'`
while :
do
if ! psaux | grep -w mysqld | grep -v grep >/dev/null 2>&1
then
/etc/init.d/mysqldstart
echo $Datemysqldwasreboot >>/var/log/reboot_mysql.log
fi
done
本篇这是以mysql为例子,但实际中如果是监控的脚本出了问题,报警没发出来,那就比较尴尬了,所以为保证我们的检查脚本能实时运行,我们需要一个进程来守护这个脚本,这就是我们今天要说的主题,如何给脚本写一个daemon,我们先上代码:
#!/usr/bin/python
import subprocess
from daemonimport runner
cmd = "/root/demo_script/restart_mysql.sh"
class App():
def __init__(self):
self.stdin_path = '/dev/null'
self.stdout_path = '/dev/tty'
self.stderr_path = '/dev/tty'
self.pidfile_path = '/tmp/hello.pid'
self.pidfile_timeout = 5
def start_subprocess(self):
return subprocess.Popen(cmd, shell=True)
def run(self):
p = self.start_subprocess()
while True:
res = p.poll()
if resis not None:
p = self.start_subprocess()
if __name__ == '__main__':
app = App()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()
脚本比较简单,没什么特别的逻辑,关于daemon这个模块如何使用,我这里给出官方的解释,注意哟,是英文的,我就不翻译了,如果有生词就查查字典,就当多学几个了单词吧。
__init__(self, app)
| Setuptheparametersof a new runner.
|
| The `app` :
|
| * `stdin_path`, `stdout_path`, `stderr_path`: Filesystem
| pathsto openand replacetheexisting `sys.stdin`,
| `sys.stdout`, `sys.stderr`.
|
| * `pidfile_path`: Absolutefilesystempathto a filethat
| willbeusedas thePIDfilefor thedaemon. If
| ``None``, noPIDfilewillbeused.
|
| * `pidfile_timeout`: Usedas thedefault acquisition
| timeoutvaluesuppliedto therunner's PIDlockfile.
|
| * `run`:
| started.
|
| do_action(self)
| Performtherequestedaction.
|
| parse_args(self, argv=None)
| Parsecommand-linearguments.
这样就完成了,守护进程的启动比较高大上,输入以上代码后,可以直接在终端输入:
#python monitor.py start
当然还有stop,restart等参数。
这里我介绍的是其中一个应用场景,实际中可以灵活运用,比如1台服务器上启动的程序过多,环境配置比较复杂,就可以先启动daemon进程,然后通过daemon来启动其它所有应用程序,就不用一个一个应用程序启动了,这篇就到这里,有问题可以给我留言。
⑺ 如何保护一个程序执行,不被任何打断或改变
防止被干掉,比较麻烦,如果有源代码,尽可能编译成服务程序、驱动程序。
如果是现成的可执行文件,设置可执行文件的权限如系统权限,将杀进程的权限降低
另外,可以设置计划任务,定时启动
还可以用双进程方式,检查一个被杀死,另一个立即启动,等等。