# Auto restart service after cron update



## nforced (Oct 26, 2014)

I have set[]up a cron job to update my jails with `/usr/local/sbin/portmaster -dB --delete-packages --no-confirm -a`. 

This is working just great but there is something that is missing. For example today Dovecot updated to version 2.2.15 but the service was not restarted and this caused lots of error messages. This applies to any service updated this way so I am looking for a way to overcome this, does anyone have a practical resolution for this scenario?


----------



## wblock@ (Oct 26, 2014)

Automatically updating ports like that is prone to trouble.  It assumes that there will be no manual steps on /usr/ports/UPDATING, which is not true.

Auto-restarting services is a similar situation.  There are times when a service should be stopped before an update and restarted afterwards.  There are times when the service should not be restarted after an update until the administrator has had a chance to check the configuration.  An automatic system does not really have a way to tell.


----------



## nforced (Oct 26, 2014)

Right, there are downsides but this is in some way dependent on the set of services in question running on a given machine also in my case it's not critical. Output is sent to an email so I review it every time just to make sure no further action is needed.
I can write a script which reads the ports-mgmt/portmaster output and search for different patterns like Upgrade of dovecot2 and perform restart on match

```
===>>> The following actions were performed:
Upgrade of dovecot2-2.2.14_1 to dovecot2-2.2.15
```

This should work and will give control which services to restart. I'll give it a try (;


----------



## nforced (Oct 26, 2014)

I wrote a little sh script (attached) to fit my needs, I know there is a better way to write this but my sh() is little rusty 


```
#!/bin/sh

for service in 'Upgrade of dovecot2' 'Upgrade of postfix'
do
 if grep "$service" /var/log/distup-cron.log > /dev/null;
 then
  printf "\n$service found! \nAuto-restart attempt\n"

  case $service in

  *dovecot2*)
  printf "Restaring dovecot2 now!\n"
  service dovecot restart
  printf "Done\n"
  ;;

  *postfix*)
  printf "Restaring Postfix now!\n"
  service postfix restart
  printf "Done\n"
  ;;

  *) printf "No matching service found for \'$service\'!\n";;
  esac

  printf "\n"
 fi
done
exit 0
```

Example output:

```
root@mail:~ # ./auto-restart-portmaster

Upgrade of dovecot2 found!
Auto-restart attempt
Restaring dovecot2 now!
Stopping dovecot.
Waiting for PIDS: 30860.
Starting dovecot.
Done


Upgrade of postfix found!
Auto-restart attempt
Restaring Postfix now!
postfix/postfix-script: stopping the Postfix mail system
postfix/postfix-script: starting the Postfix mail system
Done
```


----------

