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

June 2, 2008

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

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/”

Step 2:   Make the script an Executable

$ chmod +x

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/

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

c)  Check to see cron job is set

$ crontab -l

That’s It!


10 Responses to “How to write a simple Shell Script and Run at midnight every day?”

  1. aaa Says:

    GREAT! thanks

  2. John Morrissey Says:


    Your script was easy. I can expand on that one.
    Can you help me here?

    when my mac boots up I can have a startup a script launch. I need help so it can access another executable script on a server.

    Can you provide me with the syntax for this? What type of remote server can I access? Please provide different syntaxes for that.

    My Mac has scriptA and the server has scriptB


  3. Anonymous Says:


  4. Kenny Says:


    Please tell me how to write a shell script to run a php file in a certain location on the server. I have a go daddy server so i have to add the shell script to the cron tab thought cpanel

  5. Deepika Panda Says:

    Ohhhhh It’s an amazing description. I am very new to this field still i caught the logic easily as it has been described minutely, focusing on each & every steps.

  6. Deepika Panda Says:

    Friends, I am trying to make it more clear so that it ll be understandable for a beginner also.
    In step (a) while Editing Current Cronjob; “59 23 * * * /home/” has been set to run the script daily at 12:59 PM. So here the syntax indicates :

    .—————- minute (0 – 59)
    | .————- hour (0 – 23)
    | | .———- date (1 – 31)
    | | | .——- month (1 – 12)
    | | | | .—- day of week (0 – 6) (Sunday=0 or 7)
    | | | | |
    1st 2nd 3rd 4th 5th 6th
    ‘– sh script to be executed (with proper path)

    So every day, running at 5:15 AM would be …

    15 5 * * * filename

  7. Thanks simple and useful!

  8. Anonymous Says:

    good one

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: