System Status
Buy Now
  • Email & Apps
  • Office 365
Login
  • MyRackspace Portal
  • Cloud Control Panel
  • Rackspace Webmail Login
  • Cloud Office Control Panel
  • Support Home
  • How-To
  • Developer Documentation
  •  Blogs 
    • Expert Insights Tech Blog
    • Rackspace Blog
    • Solve: Thought Leadership

Support Network

End-to-End Multicloud Solutions.   Solving Together.â„¢   Learn more at Rackspace.com

How–To Home

Cloud Servers

  • Introduction
  • FAQ
  • All Articles

Set up cron jobs for action scripting

Last updated on:  2022-05-12

Authored by:  Rackspace Support


This article describes how to set up directories and scripts to have cron run different actions at regular times.

This article uses the pico text editor, but you can use any text editor that you want, such as vi, vim or nano.

Set up system scripts and directories

Use the following commands to set up the system scripts and directories for cron to run:

    [user@server ~]$ sudo mkdir /usr/local/sbin/continuous.active
    [user@server ~]$ sudo mkdir /usr/local/sbin/continuous.inactive
    [user@server ~]$ sudo mkdir /usr/local/sbin/hourly.active
    [user@server ~]$ sudo mkdir /usr/local/sbin/hourly.inactive
    [user@server ~]$ sudo mkdir /usr/local/sbin/daily.active
    [user@server ~]$ sudo mkdir /usr/local/sbin/daily.inactive
    [user@server ~]$ sudo mkdir /usr/local/sbin/weekly.active
    [user@server ~]$ sudo mkdir /usr/local/sbin/weekly.inactive

Set up script for continuous jobs

Run the following command to create the script file for continuous jobs and open it in a text editor:

    [user@server ~]$ sudo pico /usr/local/sbin/continuous.sh

Insert the following script to run each container and all scripts held inside at the specified interval:

    #!/bin/bash
    ACTIVE_SCRIPTS_DIR=/usr/local/sbin/continuous.active
    for module in `find "$ACTIVE_SCRIPTS_DIR" -maxdepth 1 -mindepth 1 -type f`; do
        if [ -x $module ]; then
            $module
        fi
    done

Run the following command to make the continuous.sh script executable:

    [user@server ~]$ sudo chmod u+x /usr/local/sbin/continuous.sh

Set up script for hourly jobs

Run the following command to create the script file for hourly jobs and open it in a text editor:

    [user@server ~]$ sudo pico /usr/local/sbin/hourly.sh

Insert the following script to run each container and all scripts held inside hourly:

    #!/bin/bash
    ACTIVE_SCRIPTS_DIR=/usr/local/sbin/hourly.active
    for module in `find "$ACTIVE_SCRIPTS_DIR" -maxdepth 1 -mindepth 1 -type f`; do
        if [ -x $module ]; then
            $module
        fi
    done

Run the following command to make the hourly.sh script executable:

    [user@server ~]$ sudo chmod u+x /usr/local/sbin/hourly.sh

Set up script for daily jobs

Run the following command to create the script file for daily jobs and open it in a text editor:

    [user@server ~]$ sudo pico /usr/local/sbin/daily.sh

Insert the following script to run each container and all scripts held inside daily:

    #!/bin/bash
    ACTIVE_SCRIPTS_DIR=/usr/local/sbin/daily.active
    for module in `find "$ACTIVE_SCRIPTS_DIR" -maxdepth 1 -mindepth 1 -type f`; do
        if [ -x $module ]; then
            $module
        fi
    done

Run the following command to make the daily.sh script executable:

    [user@server ~]$ sudo chmod u+x /usr/local/sbin/daily.sh

Set up script for weekly jobs

Run the following command to create the script file for weekly jobs and open it in a text editor:

    [user@server ~]$ sudo pico /usr/local/sbin/weekly.sh

Insert the following script to run each container and all scripts held inside weekly:

    #!/bin/bash
    ACTIVE_SCRIPTS_DIR=/usr/local/sbin/weekly.active
    for module in `find "$ACTIVE_SCRIPTS_DIR" -maxdepth 1 -mindepth 1 -type f`; do
       if [ -x $module ]; then
           $module
       fi
    done

Run the following command to make the weekly.sh script executable:

    [user@server ~]$ sudo chmod u+x /usr/local/sbin/weekly.sh

Add scripts to the crontab file

A crontab file contains a list of commands meant to be run at specified times. Use the following command to edit the crontab file:

    [user@server ~]$ sudo crontab -e

Each line in the crontab file has five date and time fields in the following format:

    minute (0-59) hour (0-23) day (1-31) month (1-12) weekday (0-6)

If you want to run a script some number of times per time unit, you can use the format */n, where n is the number of times that you want the script to run.

Add the following entries to the crontab file:

    MAILTO= youremail@domain.com

    */5 * * * * /usr/local/sbin/continuous.sh
    6 */1 * * * /usr/local/sbin/hourly.sh
    16 02 * * * /usr/local/sbin/daily.sh
    26 03 * * 0 /usr/local/sbin/weekly.sh

Use the Feedback tab to make any comments or ask questions. You can also [start a conversation with us](https://www.rackspace.com/contact).

Share this information:

©2020 Rackspace US, Inc.

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License

See license specifics and DISCLAIMER

About Rackspace
  • About
  • Customer Stories
  • Events
  • Programs
Blogs
  • The Rackspace Blog
  • Expert Insights Tech Blog
  • Solve: Thought Leadership
  • News
  • Contact Information
  • Legal
  • Careers
Site Information
  • Style Guide for Technical Content
  • Trademarks
  • Privacy Statement
  • Website Terms
Support Network
  • Support Network Home
  • Rackspace How-To
  • API Documentation
  • Developer Center
  • ©2020 Rackspace US, Inc.