How to Schedule a Linux Job Without Cron

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to Schedule a Linux Job Without Cron

    Scheduling a job/command in Linux is acronym to cron. Whenever we need to schedule a job, we call cron, but do you know we can schedule a job at a later time without corn? You can do it as suggested below.
    Run a command (say date) every 5 sec and write the output to a file (say date.txt). To achieve this scenario, we need to run the below one liner script directly on the command prompt.

    Code:
    $ while true; do date >> date.txt ; sleep 5 ; done &
    Anatomy of the above one liner script:
    1. while true Ask script to run while the condition is true, it acts as a loop which makes the command to run again-and-again or say in a loop.
    2. do do perform what follows, ie., execute command or set of commands that lies ahead of do statement.
    3. date >> date.txt here the output of date command is being written to a file date.txt. Also note that we have used >> and not >.
    4. >> ensures that the file (date.txt) is not overwritten every time the script execute. It just append the changes. Whereas > overwrite the file again and again.
    5. sleep 5 It ask the shell to keep a time difference of 5 seconds before it executed again. Note the time here is always measured in seconds. Say if you want to execute the command every 6 minutes, you should use (6*60) 360, in succession of sleep.
    6. done marks the end of while loop.
    7. & Put the whole process in loop to background.
    Similarly, we can execute any script in the same manner. Here is the command to call a script after certain interval (say 100 sec) and the name of script is script_name.sh.

    Also worth mentioning that the script above should be run in the directory where the script to be called lies, else you need to provide full path (/home/$USER//script_name.sh). The syntax for calling script at above described interval is:
    Code:
    $ while true; do /bin/sh script_name.sh ; sleep 100 ; done &
    For more details, check: How to Set up Cron Jobs in cPanel
    Last edited by Rsync; 31-05-2022, 06:07.

    #2
    @gauravd​ Thanks for sharing such a useful information and providing detailed explanation

    Comment

    Working...
    X