Skip to content

Instantly share code, notes, and snippets.

@peycho
Last active April 26, 2019 14:01
Show Gist options
  • Save peycho/6c7432d61ab82cab3e828378bae06f25 to your computer and use it in GitHub Desktop.
Save peycho/6c7432d61ab82cab3e828378bae06f25 to your computer and use it in GitHub Desktop.
Monitoring linux process with bash script.
#!/bin/bash
process="your-process-here"
run_command="command to start the process"
time_check="5" # time to check in sec.
while true ; do
proc_count=$(ps xa | grep -v grep | grep "$process" | wc -l)
if [ "$proc_count" == 0 ]; then
$run_command
echo "$(date) process $process was restarted."
fi
sleep $time_check
done
exit 0
@peycho
Copy link
Author

peycho commented Apr 26, 2019

To start it in screen

screen -S process-checker
chmod +x procx.sh
./procx.sh
ctrl+a+d

@peycho
Copy link
Author

peycho commented Apr 26, 2019

Systemd config
Create example config in /etc/systemd/system/procx.service

[Unit]
Description=procx
Requires=network-online.target
After=network-online.target

[Service]
User=youruser
Group=yourgroup
Restart=on-failure
LimitAS=infinity
LimitRSS=infinity
LimitCORE=infinity
LimitNOFILE=999999
Environment=LANG=en_US.UTF-8
Environment=LC_ALL=en_US.UTF-8
SyslogIdentifier=procx
ExecStart=/path/to/procx.sh
ExecReload=/bin/kill -HUP $MAINPID
KillSignal=SIGINT

[Install]
WantedBy=multi-user.target

Enable the service

sudo systemctl enable procx.service
sudo systemclt start procx.service

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment