-1

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
Muhammad Hassan
  • 253
  • 1
  • 6
  • 14
  • Yes, and you can press enter at that prompt, since you are familiar with nano. – muru Aug 19 '14 at 08:18
  • Uh, select 2, which is nano. – muru Aug 19 '14 at 08: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 08:23
  • Not ./ShellScript.sh, but the full path. – muru Aug 19 '14 at 08: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
  • /15 will run it every* 15mn, this is not the same as to start 15mn after the last run has finished. – Emmanuel Aug 19 '14 at 15:21
  • Downvoted because I think the OP is just asking us to solve his problem rather than wanting to learn the solution. Also this question has been solved in the duplicate. – don.joey Aug 19 '14 at 15:30
  • Can you explain in what why this is different from a normal cron job? – don.joey Aug 19 '14 at 15:32
  • First: you should not edit your question to contain the answer; second: the downvote was to motivate you to explain (by editing) why your question was different. It would be reverted on the edit. That said, you are right: they are only internet points. Third, it is absolutely permissable to ask a pseudo-duplicate question if you can prove why it is different. Anyway, best of luck. – don.joey Aug 19 '14 at 19:10

1 Answers1

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.

Jacob Vlijm
  • 83,767
  • @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 09:22