openWRT BASH 脚本查询进程是否运行,以及未运行时重启

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42396877/article/details/82836552

系统: openWRT v15, CHAOS CALMER

脚本: BASH


步骤:

1. 更新数据源及安装 BASH;

#opkg update
#opkg install bash

2. 脚本文件, check_process.sh

#!/bin/bash

while [ 1 ]
do
        flag=$(ps | grep check_process | grep -v "grep" | wc -l)
        if [ $flag = "1" ]
        then
                echo "check_process RUNNING!"
        else
                echo "check_process has stopped."
                #check_process start up
        fi

        sleep 5
done

 注意事项: 进程名 check_process 的唯一性,否则 if 比较 "1" 不正确

4. 把脚本设为开机启动;

4.1 新建启动文件:
    #vi /etc/init.d/usart_1_run_monitor
 
4.2 文件内容:
#!/bin/sh /etc/rc.common
#/etc/init.d/usart_1_run_monitor
START=82
start() {
    #指定目录下的 bash 脚本,check_process.sh
}
 
stop(){
    killall usart_1_run_monitor
}
 
4.3 修改文件属性:
    #chmod 777 /etc/init.d/usart_1_run_monitor
 
4.4 启用开机启动服务
    #/etc/init.d/usart_1_run_monitor enable

5. 也可在 /etc/crontab 中进行定时检查

展开阅读全文

判断一个进程是否运行

08-16

贴出代码如下:rn#include rn#include rn#include rn#include rnrnusing namespaces std;rnrnvoid main()rnrn string M_ProcessID="qq.exe";rn DWORD lpidProcesses[1024], cb = 0, cbNeeded = 0;rn HMODULE lphMods[1024];rn HANDLE hProcess = NULL;rn char szProcessID[MAX_PATH];rnrn memset(lpidProcesses, 0, sizeof(lpidProcesses));rn memset(lphMods, 0, sizeof(lphMods));rn memset(szProcessID, 0, sizeof(szProcessID));rnrn if ( !::EnumProcesses( lpidProcesses, sizeof(lpidProcesses), &cb ) )rn rn cout << " error " ;rn return ;rn rnrn for(int i=0; i< (int) (cb / sizeof(DWORD)); i++)rn rn memset(szProcessID, 0, sizeof(szProcessID));rn hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, lpidProcesses[i]);rn ::EnumProcessModules(hProcess, lphMods, sizeof(lphMods), &cbNeeded);rn ::GetModuleFileNameEx( hProcess, lphMods[0], szProcessID,sizeof(szProcessID));rn CloseHandle(hProcess);rn rnrn if (!strcmp(szProcessID,M_ProcessID))rn rn cout << "The process has opened!" ;rn rn elsern rnrn cout <<"The process need to open!" << endl;rn rnrnrnrn错误提示:rn1>c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(226) : error C2146: syntax error : missing ';' before identifier 'Internal'rn1>c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(226) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-intrn1>c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(226) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-intrn……………………rn(下面都是类似的错误)rnrnwinbase.h文件中的部分:rntypedef struct _OVERLAPPED rn ULONG_PTR Internal; //226行rn ULONG_PTR InternalHigh;rn union rn struct rn DWORD Offset;rn DWORD OffsetHigh;rn ;rnrn PVOID Pointer;rn ;rnrn本人小白,rn代码也是参考网上的弄出来的,错误提示让我很不明白,应该是和是否用MFC有关……rn还望大神指导! 论坛

没有更多推荐了,返回首页