Trigger a bash script at startup to execute periodically - linux

I have made a bash script, let's call it script.sh, which has the next sctructure:
#!/bin/bash
while true
do
do_something()
sleep 1800 #seconds
done
I want the script to run as a task at startup although there is no user connected to the system. I thought that I could use 'nohup script.sh' but I don't know if I can use it at startup without any user connected. Have anybody some idea?

Look into using /etc/cron.hourly/ for an hourly script. It will run hourly at some interval past the hour. On RHEL, this is defined in /etc/cron.d/0hourly as 1 minute past the hour.
You could then extend this framework for half-hour intervals (1800s = 30 minutes), e.g., in /etc/cron.d/1_halfhourly:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
00,30 * * * * root run-parts /etc/cron.halfhourly
And put your script, or symlink it, in /etc/cron.halfhourly.
Naturally, this could be extended right down to one minute intervals, e.g., in /etc/cron.d/2perminute:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
* * * * * root run-parts /etc/cron.perminute
This would run every script under /etc/cron.perminute each minute.

Related

Not able to run a python script after every 5mins using cron

I edited the crontab with -e option. Then I went to /etc/cron.d directory to create a file which would run the process. Again I edit the /etc/crontab file. But I couldn't made it run. I refered to this article from stackoverflow and did exactly same but I don't know why cron is not working for me ?
here is how my crontab looks like -
# 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
*/5 * * * * anikde /home/anikde/Documents/pythonProjects/python_scripts/*
* * * * * anikde python /home/anikde/Documents/pythonProjects/python_scripts/test/write.py
I have set the 1st job to run after every 5 minutes and 2nd job after every one minute. But none of jobs are running automatically. While they actually run when I command the job to run first script as bash script and the second file as python script.
I assume you are trying to automatically run below script every 5 minutes:
/home/anikde/Documents/pythonProjects/python_scripts/test/write.py
First, determine the location of your Python executable, using which python command. In below examples I assume the returned path to be /usr/bin/python.
If editing your own crontab (crontab -e) try this command:
*/5 * * * * /usr/bin/python /home/anikde/Documents/pythonProjects/python_scripts/test/write.py
If no user is specified, the job is run as the user that owns the crontab file, using his environment. But you can also try adding the username
*/5 * * * * anikde /usr/bin/python /home/anikde/Documents/pythonProjects/python_scripts/test/write.py
If editing root crontab (sudo crontab -e)
*/5 * * * * anikde /usr/bin/python /home/anikde/Documents/pythonProjects/python_scripts/test/write.py

Created cron job to run every 2 mint

I have configured cron job but it's not working.
I wanted to run the myfile.sh script for every 2 mint and below are my configuration in crontab.
# m h dom mon dow comman
2 * * * * /home/ubuntu/myfile.sh
myfile.sh is executable and contains below lines of code
#!/bin/bash
mysqldump -u[user] -p[password] --single-transaction --routines --triggers --all-databases > /home/ubuntu/backup_db10.sql
Is there anywhere we need to add configure anything?
You're running the script at two minutes past every hour. As in 1:02, 2:02 and so on.
You can change it to something like
*/2 * * * * /home/ubuntu/myfile.sh
to run it every two minutes.
A bit more info can be found here.

Linux bash shell script output is different from cronjob vs manually running the script

I wrote a linux bash shell script which works fine except the output when I run it manually is different than when I run it from a cronjob.
The particular command is lftp:
lftp -e "lcd $outgoingpathlocal;mput -O $incomingpathremote *.CSV;exit" -u $FTPUSERNAME,$FTPPASSWORD $FTPSERVER >> ${SCRIPTLOGFILE} 2>&1
When I run the script manually, the ${SCRIPTLOGFILE} contains a lot of info such as how many files/bytes/etc transferred. But when I run the same script from a cronjob there is no output unless there was an error (such as could not connect). I have tried various terminal output configurations but none work for this lftp command. Suggestions?
It's worth reading this:
crontab PATH and USER
In particular, cron won't set the same environment variables you're used to an interactive shell.
You might want to wrap your entire cron job up in a script, and then you can, for example, temporarily add some code like export >> scriptenvironment.txt and see what the difference is between the cron invoked script and the interactively invoked script.
Try man 5 crontab for details.
Once you know what envrionment variables you need for your script to run, you can set them in the crontab as necessary, or source at the start of your own script.
EXAMPLE CRON FILE
# use /bin/sh to run commands, overriding the default set by cron
SHELL=/bin/sh
# mail any output to `paul', no matter whose crontab this is
MAILTO=paul
#
# run five minutes after midnight, every day
5 0 * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
# run at 2:15pm on the first of every month -- output mailed to paul
15 14 1 * * $HOME/bin/monthly
# run at 10 pm on weekdays, annoy Joe
0 22 * * 1-5 mail -s "It's 10pm" joe%Joe,%%Where are your kids?%
23 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday"
5 4 * * sun echo "run at 5 after 4 every sunday"

How to reboot via cron on scheduled basis. Ubuntu 14.04

I have a very simple script that works from the command line.
#!/bin/bash
reboot
When I put a call to execute the script into root users crontab -e using the following format it does not run. It does run the first two commands, just that last one is giving me grief. I have no MTA installed as I do not need it.
*/10 * * * * service jwtpay restart
0 3 * * * bash /root/backup/mongo.backup.s3.sh kickass /root/backup >/dev/null 2>&1
0 */3 * * * bash /root/reboot.sh >/dev/null 2>&1
What am I missing?
Maybe the script is not executable... Since you use root's crontab why call the binary via a script and not the binary itself? Use the full path to the binary. It may vary on your system. Find out where it is with which reboot.
0 */3 * * * /sbin/reboot
Don't forget to restart the cron daemon, after changeing the crontab.

Cron jobs -- to run every 5 seconds

I want to create cron job that runs a script every 5 seconds. Seeing that cron jobs only allows increments of minutes 0-59 and so on.
I thought to create another script that calls my original script written below.
#!/bin/bash
while true
do
# script in the same directory as this script. is this correct?
bash makemehappy.sh
sleep 1
done
I now, need to know how to run this script every time i boot my computer and for it to start itself if it isn't running for some reason.
I am also aware that running this script every minute wouldn't be a good thing. :)
if there is an easier way to run a script every 5 seconds please advise.
Please and thank you.
I wouldn't use cron for this. I would use that bash script (use an absolute path, unless you want it to be portable and know that the directory structure will be preserved).
Instead, I would just sleep 5, just like you did (only 5 seconds instead of 1).
As far as starting it with your system, that depends on the system. On (some) Linux distros, there's a file called /etc/rc.local in which you can add scripts to run when the system starts. Well... I shouldn't be so general, the distros that I have used have this. If you're running Ubuntu, there is no longer an inittab, they use upstart, btw.
So if you have an endless loop and an entry in /etc/rc.local, then you should be golden for it to run endlessly (or until it encounters a problem and exits).
Try using anacron or, better yet, an init script to start when the computer starts.
If you want the script to "restart itself", you'll need to run something every few minutes to check the original is still running. This can be done in inittab (/etc/inittab) or, on Ubuntu, /etc/event.d. Try man 5 inittab, looking at the 'respawn' option.
Some crons have an #reboot time specifier (this covers all the time and date fields). If yours does, you can use that. If this is a "system" service (rather than something running for yourself), the other solutions here are probably better.
Init scripts are fine at boot, but don't detect if a process fails and has to be restarted. supervisord does a great job of detecting failed processes and restarting them. I'd recommend a script with a 5-second loop like #Tim described, but wrap supervisord around it to make sure it keeps running.
As explained in detail in my answer to a similar question, you can use SystemD timer units with whatever schedule that you want - down to a theoretical 1 nanosecond schedule with no sleep kludges
Quick overview:
Setup a SystemD service to run what you want - this can be as simple as:
/home/myusuf3/.config/systemd/user/makemehappy.service
[Unit]
Description=Make me happy
[Service]
ExecStart=/home/myusuf3/.local/bin/makemehappy.sh
Setup a SystemD timer with the schedule that you want, as documented in man systemd.timer:
/home/myusuf3/.config/systemd/user/makemehappy.timer
[Unit]
Description=Schedule to make me happy
[Timer]
OnBootSec=5
OnUnitActiveSec=5
AccuracySec=1
Enable and start the timer:
:
systemctl --user daemon-reload
systemctl --user enable makemehappy.timer
systemctl --user start makemehappy.timer
(after you enable it, it will autostart every time you start the computer, but you probably want to start it now anyway).
To answer the question in the title, this is how to run a cronjob every 5 seconds :
* * * * * /path/to/script.sh
* * * * * sleep 5 && /path/to/script.sh
* * * * * sleep 10 && /path/to/script.sh
* * * * * sleep 15 && /path/to/script.sh
* * * * * sleep 20 && /path/to/script.sh
* * * * * sleep 25 && /path/to/script.sh
* * * * * sleep 30 && /path/to/script.sh
* * * * * sleep 35 && /path/to/script.sh
* * * * * sleep 40 && /path/to/script.sh
* * * * * sleep 45 && /path/to/script.sh
* * * * * sleep 50 && /path/to/script.sh
* * * * * sleep 55 && /path/to/script.sh
It's not beautiful, but this solution comes with no extra tools nor dependencies. So if you have working cron jobs, this should work right away.

Resources