Is there a better/more efficient/correct way to restart things automatically?

I have two things I run on my server that don't come as regular services or whatever. They crash occasionally and also don't autostart on boot (I'm not sure how to do that), so I have a couple little scripts I wrote that cron just runs every five minutes to run the start command if it isn't running. It works fine (I think) but I wondered if there was a better way to do this, or like an official freebsd way.

This is the script I use, in this case the one for deluged:
Code:
if [ -n "$(ps auxw | grep deluged |grep -v grep)" ]
then
# process is running 
  exit 0     
else
# process is not running
  su - username -c deluged
  exit 0
fi
exit 0
 
Have a look at sysutils/daemontools. It's the traditional way of doing what you want.

As for your script, have a look at pgrep(1), it's a bit neater to use instead of the ps | grep | grep -v grep people generally use.
 
You might consider monit as well. https://mmonit.com/monit/ (I know there's a port)
Sample from a named server.
Code:
set daemon  60
set mailserver localhost
set alert supportteam@nyi.net

set httpd port 2812 and
    use address localhost  # only accept connection from localhost
    allow localhost        # allow localhost to connect to the server and
    allow admin:monit      # require user 'admin' with password 'monit'
    allow @monit           # allow users of group 'monit' to connect (rw)
    allow @users readonly  # allow users of group 'users' to connect readonly

check process named with pidfile /var/run/named/pid
  start program = "/usr/local/etc/rc.d/named start"
  stop program = "/usr/local/etc/rc.d/named stop"
 
You might want to look at daemon(8). It is part of the base of FreeBSD and it starts and supervises other processes, perhaps restarting it when necessary. Super visioning is done with almost no extra costs, because daemon starts the other processes as childs of its own, and hence knows automatically when the child has stopped working, and in that case may immediately restart it.

With respect how to start it up, there are 2 options. When I am not lazzy, and want to do everything in the most correct way, I write a rc script. Sometimes, if it is only a single start command without extra bells and whistles, I put this into /etc/rc.local.
 
Have a look at sysutils/daemontools. It's the traditional way of doing what you want.

As for your script, have a look at pgrep(1), it's a bit neater to use instead of the ps | grep | grep -v grep people generally use.

this looks like just what I need, thanks.

First thing to do would be to understand why they are crashing and possibly solve that issue.
Did you built them yourself ?

No, just installed with pkg from the binary repository. It's only one of the two that ever crashes though, and I'm not that worried about it.

With respect how to start it up, there are 2 options. When I am not lazzy, and want to do everything in the most correct way, I write a rc script. Sometimes, if it is only a single start command without extra bells and whistles, I put this into /etc/rc.local.

rc.local I used a lot when I was on rhel. I didn't know it worked on FreeBSD as well.

Code:
ps auxw|grep [d]eluged
Enclosing the first letter in brackets will perform the same action as `grep -v grep` .

Neat trick, thanks
 
Back
Top