Question

Daily Crontab Jobs

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


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

KFSys
Site Moderator
Site Moderator badge
February 27, 2021
Accepted Answer

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.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.