1

I have amule-daemon installed on Debian 11 WSL. Process amuleweb crashes every now and then so I need to restart it.

I'm trying to automate this with the following bash script:

#!/bin/bash

if [[ $(pgrep amuleweb) ]]; then
   echo "amuleweb is running..";
else
   echo "amuleweb is not running, restarting now..";
   amuleweb --amule-config-file=/home/debian/.aMule/amule.conf
fi

but no matter if amuleweb is running or not (I'm checking with "ps aux | grep amule"), the script always returns "amuleweb is running.."

This is an example with amuleweb stopped.

debian@CUSPIDE:~$ ./amuleweb.sh
amuleweb is running..
debian@CUSPIDE:~$ [[ $(pgrep amuleweb) ]]
debian@CUSPIDE:~$ echo $?
1

If I run the pgrep command directly from a terminal, it works, but within my script it always returns "0".

How do I fix it? Thanks

1
  • You should just if pgrep -a amuleweb instead. After debugging, you can change it to if pgrep amuleweb > /dev/null if you want. Probably you need -x.
    – Tom Yan
    Commented Dec 18, 2022 at 15:29

1 Answer 1

1

Taking an educated guess here that the name of your script might include the string amuleweb. If this is the case, then pgrep is finding the running script. Best practice here would be to use the pgrep -f (or -x, as @TomYan pointed out in the comments) form to make sure you are matching on the desired executable.

You might also consider the user of a process manager for this type of scenario (restarting a failed service) rather than rolling your own. While WSL now includes the ability to use Systemd, that would probably be overkill here unless you already have it enabled for other reasons. Take a look at Supervisord as a fairly lightweight, easy-to-configure process supervisor that can restart failed services.

1
  • Thanks, that was it, i.e. name of the script :)
    – roughnecks
    Commented Dec 18, 2022 at 15:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .