Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am new to linux and shell scripting, can anyone help me create a .sh file to execute automatically every 5 hrs without using cron job
The right solution here is definitely cron, since it's specifically designed to handle periodic execution. If for some reason you need to avoid using it, here are some simple examples of scripts that repeatedly execute a fixed command.
Here's a naive solution that gets the job done.
#!/bin/sh
while true; do
my_command
# sleep 5 hours
sleep $((5 * 60 * 60))
done
This will run the command every five hours if the time that the command runs is negligible, since the job is run synchronously. If the command takes one hour to run, then it will be every six hours.
If you need it to run every five hours exactly, you can do the following.
This command creates a lockfile in /var/run that exists when the job is running and is deleted when the job stops. This prevents multiple instances of the job from running at the same time. The main loop will wait for five hours for my_command to finish after it's been started, and will check every 60 seconds to see if the lockfile has been removed.
#!/bin/sh
lockfile=/var/run/my_command.lock
[ -f "$lockfile" ] && {printf 'lockfile already exists\n'; exit 1;}
while true; do
if [ ! -f "$lockfile" ]; then
touch "$lockfile"
(my_command ; rm "$lockfile") &
sleep $((5 * 60 * 60))
else
printf '%s\n' "skipping! process is already running"
sleep 60
fi
done
You could use at command inside your script to reschedule it again after certian time.
You could put something like this at the end of your .sh script
at now +5 hours ~/myscript.sh
So every time your script finish execution it will be rescheduled to execution after 5 hours. This approach has its downsides and using cron would be the ideal way.
Related
I can't wrap my head around this concept
If a cron job is set to execute a particular script every 5 minutes and the task the script is performing happens to be very long
What will happen if cron job re execute the script after 5 minutes and the script is not done executing the prior task will this cause the prior task to abort and restart again
Or will the server know its suppose to queue it and wait for the prior task before re executing the script
It is neither. The two tasks overlap, and run at the same time.
Cron jobs don't cancel each other unless they try to lock and access the same files, or the scripts have some other conflicts. You can of course make them cancel each other if you want, though.
The server is none the wiser, and the tasks don't wait for previous executions unless you explicitly add such details, for example, by using flock or pgrep to check for previous executions still running.
The following example entry in cron tab checks if the script is already running, and only runs if the script (cron.sh) is not already running.
* * * * * /usr/bin/pgrep -f /path/to/cron.sh > /dev/null 2> /dev/null || /path/to/cron.sh
I wanted to run a particular function in .bashrc script file ( which actually does a job of removing a docker exited containers in the background)
I already looked into cron but it is not useful for me please suggest any other methods to do it.
I also tried writing a while loop along with sleep which is not the efficient method as we start it every time and stop it.
First choice is cron, but you can also use at.
Here is a little example. The script is started once per minute and loggt each run into logfile.dat
#!/bin/bash
echo "bash $0" | at now +1 minutes -M
date >> /tmp/logfile.dat
With atq you can see witch jobs waiting for next run an with atrm you can stop the cycle.
==> man at
I don't necessarily consider this a great idea either, but to answer the question you asked...
Here's a simple template you should be able to adapt.
chime() {
local chimeDelay=10 # seconds, adjust to your needs
echo "bong!"; date; # code that Does The Thing
sleep $chimeDelay && chime & # snooze and Do The Thing again
} >/tmp/chimelog 2>/tmp/chime.err # logs, not your console
Once you execute this it should keep spawning as long as you are logged in, but ought to collapse on a HUP, which I assume is what you wanted. If you just wanted a cron substitute, then write and run it as a simplistic daemon with a HUP trap, but you probably should add locks to keep multiple instances from running, etc.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I'm terribly new to cron jobs, but one of the wordpress plugins I'm using relies on one to function the way we want it to (automatic reporting instead of manual).
It's telling me to go start a cron job and give it the command
wget --quiet /wp-admin/admin-ajax.php?action=THE_PLUGINS_FUNCTION
Okay, I did that, and I set it every minute so the whole command looks like this
(* * * * *) wget --quiet /wp-admin/admin-ajax.php?action=THE_PLUGINS_FUNCTION
of course, the query string isn't THE_PLUGINS_FUNCTION, and it's actual name is all lower case.
But it doesn't seem to be running?
Some More Info:
We are using wordpress on http://domain.com
We need the cron to run on all subdomains - http://test.domain.com
The Cron Job doesn't need to run on the main site
We are using CPanel
I'm not sure what a --quiet cron job is, and I'm also not sure of the difference between like wget and curl, again I know 'zero' about cron jobs
Let me know if you need any more information
EDIT: I removed the --quiet and got this in my email spam folder:
" /wp-admin/admin-ajax.php?action=repagent_email_reports: Scheme missing. "
Cron jobs are nothing mysterious. All a cron job is, is a time interval and a shell command. At every time interval, the shell command will be run.
The cron job you have supplied here consists of these two parts:
(* * * * *) denotes the time interval. This is five fields separated by a space. The fields denote, in order: minute, hour, day, month, and weekday. If you specified a number in one of the fields, such as 1 in the minute field, it would only run the cron job when the current minute was 1. A 10 in the hour field would only run when the current hour was 10. A * means that any value is fine. Five stars means to run the command once every minute of every hour or every day of every month. Curiously, if you had * 10 * * * it would run the cron job every minute of the 10th hour of every day of every month.
wget --quiet /wp-admin/admin-ajax.php?action=THE_PLUGINS_FUNCTION is the shell command to run. This is just a shell command. You can test the shell command outby running it in a shell.
The problem you have is that your arguments to wget are not correct. The --quiet argument to wget means that it will not produce much output. The /wp-admin/admin-ajax.php?action=THE_PLUGINS_FUNCTION is the address to request. wget is failing because /wp-admin/admin-ajax.php?action=THE_PLUGINS_FUNCTION is not a valid address - the schema (http:) and hostname (test.domain.com) are missing.
You can fix your cron job by changing it to:
(* * * * *) wget --quiet http://test.domain.com/wp-admin/admin-ajax.php?action=THE_PLUGINS_FUNCTION
You will need to add a new cron job for every subdomain you want to run this command on.
You can check to make sure you have your wget command working correctly by pasting it in a shell and running it. Once you know it is working fine, add it as a cron job and it should work just fine.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Bash script that kills a child process after a given timeout
Is there a way to write a shell script that would execute a certain command for 15 seconds, then kill the command?
I have tried sleep, wait and ping but maybe I am using them wrong.
Use the timeout command:
timeout 15s command
Note: on some systems you need to install coreutils, on others it's missing or has different command line arguments. See an alternate solution posted by #ArjunShankar . Based on it you can encapsulate that boiler-plate code and create your own portable timeout script or small C app that does the same thing.
Some machines don't have timeout installed/available. In that case, you could background the process; its PID then gets stored as $!; then sleep for the required amount of time, then kill it:
some_command arg1 arg2 &
TASK_PID=$!
sleep 15
kill $TASK_PID
At this URL I find that there are mentioned, more than one solutions to make this happen.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
How do I set up cron to run a file just once at a specific time? One of the alternatives is at but it is not accessible to all users on standard hosting plans. Therefore I was wondering whether there is way to do it using cron?
You really want to use at. It is exactly made for this purpose.
echo /usr/bin/the_command options | at now + 1 day
However if you don't have at, or your hosting company doesn't provide access to it, you can have a cron job include code that makes sure it only runs once.
Set up a cron entry with a very specific time:
0 0 2 12 * /home/adm/bin/the_command options
Next /home/adm/bin/the_command needs to either make sure it only runs once.
#! /bin/bash
COMMAND=/home/adm/bin/the_command
DONEYET="${COMMAND}.alreadyrun"
export PATH=/usr/bin:$PATH
if [[ -f $DONEYET ]]; then
exit 1
fi
touch "$DONEYET"
# Put the command you want to run exactly once here:
echo 'You will only get this once!' | mail -s 'Greetings!' me#example.com
Try this out to execute a command on 30th March 2011 at midnight:
0 0 30 3 ? 2011 /command
WARNING: As noted in comments, the year column is not supported in standard/default implementations of cron. Please refer to TomOnTime answer below, for a proper way to run a script at a specific time in the future in standard implementations of cron.
You really want to use at. It is exactly made for this purpose.
echo /usr/bin/the_command options | at now + 1 day
However if you don't have at, or your hosting company doesn't provide access to it, you could make a self-deleting cron entry.
Sadly, this will remove all your cron entries. However, if you only have one, this is fine.
0 0 2 12 * crontab -r ; /home/adm/bin/the_command options
The command crontab -r removes your crontab entry. Luckily the rest of the command line will still execute.
WARNING: This is dangerous! It removes ALL cron entries. If you have many, this will remove them all, not just the one that has the "crontab -r" line!
You could put a crontab file in /etc/cron.d which would run a script that would run your command and then delete the crontab file in /etc/cron.d. Of course, that means your script would need to run as root.
Your comment suggests you're trying to call this from a programming language. If that's the case, can your program fork a child process that calls sleep then does the work?
What about having your program calculate the number of seconds until the desired runtime, and have it call shell_exec("sleep ${secondsToWait) ; myCommandToRun");
at is the correct way.
If you don't have the at command in the machine and you also don't have install privilegies on it, you can put something like this on cron (maybe with the crontab command):
* * * 5 * /path/to/comand_to_execute; /usr/bin/crontab -l | /usr/bin/grep -iv command_to_execute | /usr/bin/crontab -
it will execute your command one time and remove it from cron after that.
For those who is not able to access/install at in environment, can use custom script:
#!/bin/bash
if [ $# -lt 2 ]; then
echo ""
echo "Syntax Error!"
echo "Usage: $0 <shell script> <datetime>"
echo "<datetime> format: %Y%m%d%H%M"
echo "Example: $0 /home/user/scripts/server_backup.sh 202008142350"
echo ""
exit 1
fi
while true; do
t=$(date +%Y%m%d%H%M);
if [ $t -eq $2 ]; then
/bin/bash $1
echo DONE $(date);
break;
fi;
sleep 1;
done
Let's name the script as run1time.sh
Example could be something like:
nohup bash run1time.sh /path/to/your/script.sh 202008150300 &