Question

Daily Crontab Jobs

Posted February 27, 2021 331 views
Linux CommandsUbuntu 16.04Ubuntu 20.04

I was Looking for some daily commands to be executed on my server ubuntu 20.04. Please help me

Commands to be daily executed on night 11:25pm

sudo service apache2 restart
sudo apt update
sudo apt upgrade -y
sudo apt autoremove -y

sudo reboot

Please help me.
Thanks in advance

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
2 answers

Hi @kaustubhk24,

You correctly mentioned crontab. You can configure this as a script to run every day at 11:25pm.

Let’s first start with creatng the script. SSH to your droplet and start executing the following commands

touch ~/updateScript

The above will create the file for you. Now it’s time to edit it.

nano ~/updateScript

Inside of it, you can type in the following

#!/bin/bash

sudo service apache2 restart
sudo apt update
sudo apt upgrade -y
sudo apt autoremove -y
sudo reboot

Save the file and exit. Now time to make sure your script is executable

sudo chmod +x ~/updateScript

Now the last bit, add it to your crontab. To do that, type in

crontab -e

WHen the file opens at the bottom of it, add the following line

15 23 * * * bash ~/updateScript

Save the file and exit from it. To make sure you’ve added the cron, type in

crontab -l 

You should see the command you added at the bottom.

Regards,
KFSys

A word of caution: there are times when going through the update/upgrade process requires user input beyond the -y flag. There are times an upgrade will change a config file that would wipe out any customizations you have previously set up, or change the location of a file, etc. This has the potential to make the cron script hang or fail every day until you check on it again.

My personal recommendation is to run the update/upgrade as a manual process.

For a suggestion on a script to run daily might be to have server logs emailed to you.