Issue restarting apache with cron / crontab ( Ubuntu 18.04)

Perovic asked:

I have a specific case where testing a new version of the software is being done at a specific time of the day so I have cronjob that should:

Software is set in /var/www/html folders V2 and V3 respectively. Therefore I use sed to change the version. a) in the morning change virtual host and restart apache b) in the afternoon change back to an old version and restart apache

Crontab is set like this:

0 9 * * *  sed -i 's/V2/V3/g'  /etc/apache2/sites-available/software.conf && service apache2 restart
0 14 * * *  sed -i 's/V3/V2/g'  /etc/apache2/sites-available/software.conf && service apache2 restart 

Sed changes the version but apache doesn’t restart to apply the changes. Can you please help me fix the issue? Thanks

My answer:

The old command service is deprecated and will be removed in a future Ubuntu release. It is not the preferred way to manage services anymore. That is now systemctl.

You should instead write systemctl restart apache2. Of course, the server doesn’t need to be restarted for a simple configuration change; a reload would do as well and not cause an unnecessary service interruption. So, systemctl reload apache2.

It is also the case that crontabs have a more restricted PATH than your normal login shell. It is possible that neither systemctl nor service are in the PATH. So you may wish to set a PATH at the top of the crontab. For example:


View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.