-1

This question already has an answer here:

I have a shell script in my Ubuntu Server 14.04 LTS at ./ShellScript.sh. I setup /etc/rc.local to run the shell script after boot but before login using below code.

To run run it boot write this command in terminal: sudo nano /etc/rc.local then add following and save.

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

#!/bin/bash

./ShellScript.sh

exit 0

Note: Keep in mind that I am working in root mode. Now when I run crontab -e then I got the following. Now What to do?

no crontab for root - using an empty one
Select an editor.  To change later, run 'select-editor'.
1. /bin/ed
2. /bin/nano        <---- easiest
3. /usr/bin/vim.basic
4. /usr/bin/vim.tiny
Choose 1-4 [2]:

After selecting 2, I got crontab: "/usr/bin/sensible-editor" exited with status 2

Now I want to run/execute this shell script again and again having 15min of time interval between every run after boot but before login. So Can I do it?

Answer:

First of all, you can't access crontab -e via root. You have to login via USER account. After login in USER account, Go to terminal and type crontab -e Here you will get some text like below.

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command

Now I=in the end of file, you have to add */15 * * * * /ShellScript.sh line to run your script each after 15 min. Now save the file and its done.

Note: If you want different timing of interval to run script again and again then don't get confused. Here are some online tools that will generate Cron lines for you.

  1. http://www.cronmaker.com/
  2. http://crontab-generator.org/
  3. http://www.generateit.net/cron-job/
  4. http://cron.nmonitoring.com/cron-generator.html
  5. http://www.openjs.com/scripts/jslibrary/demos/crontab.php

marked as duplicate by muru, don.joey, Mitch Aug 19 '14 at 19:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Yes, and you can press enter at that prompt, since you are familiar with nano. – muru Aug 19 '14 at 8:18
  • Uh, select 2, which is nano. – muru Aug 19 '14 at 8:20
  • Try this: export EDITOR=/usr/bin/nano; crontab -e. Also, you can use your own crontab instead of root's. – muru Aug 19 '14 at 8:23
  • Not ./ShellScript.sh, but the full path. – muru Aug 19 '14 at 8:29
  • It is better to use the full, absolute path (one starting with /, like /etc/rc.local), when calling a script from another one (unless you are sure you control your current dir totally). If you're unsure, you can use readlink -f ./ShellScript.sh to have the absolute path. – Rmano Aug 19 '14 at 11:42
3

Couldn't you simply run it in the background, inside a while loop:

while true
 do
    [command]
    sleep [number of seconds]
 done

So the construction would be:

  • Add a line in /etc/rc.local to call your script (your ShellScript.sh) + "&" to make it exit
  • Run the commands you want the ShellScript.sh to execute in a while loop (inside Shellscript.sh):

    while true
     do
        [command_1]
        [command_2]
        [command_3]
        [command_4]
        sleep 900
     done
    

That way it would run on startup and from then on every 15 minutes

Note that if you want to run it from cron, you need to set the full path, since cron runs with a limited set of environment variables.

Example:

I created a silly script, adding a line with the string "monkey" to a file test.txt on my desktop:

the script:

#!/bin/bash

while true
 do
    echo monkey>>/home/jacob/Desktop/test.txt
    sleep 5
 done

The line in my /etc/rc.local file:

sh /home/jacob/Desktop/while.sh &

That's all.

  • @MuhammadHassan I believe this is what he meant, with 900 seconds, not 15 ;) Then just call your script in the background, i.e. ./Shellscript.sh & – Aserre Aug 19 '14 at 9:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.