How to create a cronjob

How to write a simple Shell Script and Run at midnight every day?

Task: Need to write a unix/linux shell script which moves Files from One Folder to Another Folder once a day (Say at midnight). Also set permissions for newly moved files accordingly.

Step 1: Write a Shell Script to do moving and set permissions

#!/bin/sh
mv /home/a/* /home/b/
chown nobody:nobody /home/b/*

Here I want to move files from “/home/a/” to “/home/b” and set permissions to nobody so that PHP running on Apache can do work on them. I saved the script as “/home/myscript.sh”

Step 2: Make the script an Executable

$ chmod +x myscript.sh

Step 3: Run at mid night every day by adding to Cron Job

a) Edit Current Cronjob

$ crontab -e

Then add your script entry there (works same as VI editor.. use i to go to edit mode). I am actually setting at 11.59 PM..

59 23 * * * /home/myscript.sh

b) Save cron job by exiting (using Shift+zz)

c) Check to see cron job is set

$ crontab -l

That’s It!

Posted on: Friday 05 June 2015