A
A
Anatoly2019-07-24 14:45:17
linux
Anatoly, 2019-07-24 14:45:17

How to know if a process is running?

To track file changes on the disk I use:
https://github.com/gregghz/Watcher
When I run it, everything works, but over time, it stops working.
I restart it and it works again
How to check that it is working at the current moment?
1) I check pid, it exists:

[email protected]:~$ll /root/.watcher/watcher.pid
-rw-rw-rw- 1 root root 5 июн  3 08:38 /root/.watcher/watcher.pid

2) I check the processes:
[email protected]:~$ps ax | grep watcher
1980 ?        Sl     0:01 python /usr/bin/watcher.py start
3198 pts/0    S+     0:00 grep watcher

I run it through rc-local.service, for this I created a file
/etc/rc.local#!/bin/sh -e
watcher.py stop
watcher.py start
exit 0

PS. "watcher.py stop" registered, because sometimes it gives an error that it is already running. Probably looks that PID is created.
Created a service, registered in the file
/etc/systemd/system/rc-local.service
[Unit]
 Description=/etc/rc.local Compatibility
 ConditionPathExists=/etc/rc.local
 
[Service]
 Type=forking
 ExecStart=/etc/rc.local start
 TimeoutSec=0
 StandardOutput=tty
 RemainAfterExit=yes
 SysVStartPriority=99
 
[Install]
WantedBy=multi-user.target

Gave him rights and set to run:
chmod +x /etc/rc.local
systemctl enable rc-local
systemctl start rc-local

PS. I think to put it on daily reboot in cron. But while it is interesting how to trace its "fall".

Answer the question

In order to leave comments, you need to log in

4 answer(s)
P
pfg21, 2019-07-24
@Tolly

why crowns and other crutches, if there is a systemd ??
write the appropriate unit and systemd itself will start the process and restart in case of a crash.

A
alternativshik, 2019-07-24
@alternativshik

run via supervisord.

R
Radjah, 2019-07-24
@Radjah

Is using incron without python binding not an option?

M
Moolzv Rivers, 2019-07-24
@SaddledSharp

You can use the universal cross-platform solution - psutil (pip install psutil).

import psutil
for proc in psutil.process_iter():
    name = proc.name()
    print(name)
    if name == "program.exe":
        pass

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question