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.
nano
. – muru Aug 19 '14 at 08:18nano
. – muru Aug 19 '14 at 08:20export EDITOR=/usr/bin/nano; crontab -e
. Also, you can use your owncrontab
instead of root's. – muru Aug 19 '14 at 08:23./ShellScript.sh
, but the full path. – muru Aug 19 '14 at 08:29/
, 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 usereadlink -f ./ShellScript.sh
to have the absolute path. – Rmano Aug 19 '14 at 11:42