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.
Related
I want to execute a node.js script within my project folder using cron job on my EC2 hosting. I know this question has been asked before many times on this forum and I followed the answers to reach where I am but I am having difficulties getting the result.
At my root level there are two folders: home and usr
My node lives at /usr/bin/node (which node gives this path)
My node file lives at /home/ubuntu/my-crm-app/test.js
The test.js has just one console.log("this is a test") - but I will be writing more code later on if this works.
Now if I execute /usr/bin/node /home/ubuntu/my-crm-app/test.js from anywhere it prints out the log,
In fact even if I do node /home/ubuntu/my-crm-app/test.js it prints out the log. So this means my paths for the node and project file are correct and working.
I typed crontab -e in the system and choose vim basic as my editor to write the cron job. It looks something like this:
# 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
*/1 * * * * /usr/bin/node /home/ubuntu/my-crm-app/test.js
So I want to execute the log every one minute. I save the vim file and come out of it and wait and wait and nothing happens. From what I understand my cron syntax is correct. So what seems to be the problem? Do I need to give a different path syntax for node and my test.js within the vim file?
Thank you.
This is something you can achieve by creating a script and calling it through cron.
Just create a script named cronjob.sh:
#!/bin/bash
/usr/bin/node /home/ubuntu/my-crm-app/test.js >> /var/log/my-crm-app`date`.log
Above command will run your node program and will generate the logs. You can anytime refer to these logs to see the execution of command and for any errors if any.
Your cron will look like this then :
*/1 * * * * /path/to/script/cronjob.sh
Just give permission to this file chmod +x /path/to/script/cronjob.sh
As this command will be running every minute and creating logs, just take care of removing these logs file after a certain period of time to avoid high disk utilization.
find /path/to/logs -name "<filename>*.log" -mtime +30 | xargs rm -f
Above command will find all the logs starting with the filename and more than of 30 day age and will delete it. Just add this line in your script and logs will be taken care.
Quoting this:
Cron doesn't run commands using a terminal you opened. It runs jobs in the background
If you really want to see the output, see this answer.
But that is not what you want. You want to redirect the output of your background job to some log file:
*/1 * * * * /usr/bin/node /home/ubuntu/my-crm-app/my_launcher.sh
where my_luncher.sh does
#!/bin/bash
/usr/bin/node /home/ubuntu/my-crm-app/test.js &> /var/log/my-crm-app.log
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.
I'm having some problem with crontab.
I have this job:
27 09 * * * drush #ildeposito.prod status
I want to execute the cron job every day at 9:27.
But it doesn't work.
If I try to execute the command directly in prompt it works.
What's wrong on my crontab?
If I execute "date" from shell I obtain:
mar 3 set 2013, 12.55.13, CEST
If I put the output of date in a file, with a cron job, I obtain:
Tue Sep 3 12:55:01 CEST 2013
Another: this is NOT executed every minute on 13!
* 13 * * * date >>/tmp/temp_out_hour
This works:
*/15 * * * * drush #site.prod cache-warmer --timeout=6 --hub-pages-file=/home/sergej/websites/sute/site-cache.txt
http://www.site.org
The system seems to have problems with hours.
First things first, you need to specify what "it doesn't work" actually means. Do you mean you see no output? Get no mail sent to you with the output? Typically, standard output from cron jobs will be packaged up in an email and sent to the user running the job. If you want to see what the job is outputting, you can use something like:
27 9 * * * drush #ildeposito.prod status >/tmp/temp_out 2>&1
which will write standard output an error to that file (you also don't need the leading 0 in 09).
If a command works from a shell prompt and not from cron, it's usually a difference in your environment settings. You can test this by replacing your job with a very simple one:
27 9 * * * date >>/tmp/temp_out
and examining that file after it executes. If the date works but drush doesn't, your problem lies outside of cron itself.
cron starts processes with a minimal set of environment variables and you have to ensure you've configured a suitable set. See, for example, here:
We use /usr/bin/env to run drush so that we can set up some necessary environment variables that drush needs to execute. By default, cron will run each command with an empty PATH, which would not work well with drush.
When running drush in a terminal, the number of columns will be automatically deteremined by drush by way of the tput command, which queries the active terminal to determine what the width of the screen is. When running drush from cron, there will not be any terminal set, and the call to tput will produce an error message. Spurrious error messages are undesirable, as cron is often configured to send email whenever any output is produced, so it is important to make an effort to insure that successful runs of cron complete with no output.
In some cases, drush is smart enough to recognize that there is no terminal -- if the terminal value is empty or "dumb", for example. However, there are some "non-terminal" values that drush does not recognize, such as "unknown." If you manually set COLUMNS, then drush will repect your setting and will not attempt to call tput.
Other than that, there are numerous other problems that can plague cron:
having a command on the final line of the file with no newline at the end sometimes causes troubles.
having % in your command somewhere.
having had the cron daemon start with a different timezone setting.
That last one can sometimes be fixed just by restarting the daemon. Given your comments about the bizarre nature of the non-performance, I'd suggest that as a first step.
I've recently started a simple project, just to help me learn the BASH scripting language a little better. Anyway, this script is set to alternate/rotate the user's desktop background/wallpaper at a given interval.
Given that this task would most likely be done every 30 minutes to 1 hour, how should I go about doing this. Would 30 minute/1 hour timers be very inefficient? Or, could cronjobs do a better job for me?
Also, how could I get this script to run in the background, so that a terminal window is not always required to be open?
Could you provide some sort of an idea into the syntax, if you can, as well.
This would be a suitable job for cron. cron would take care of invoking the script at regular intervals. You would not then have to be concerned in your script when the script should run and managing a script running in the background.
Running in the background would be extravagent as the script does not need to do much - not much more than change the current desktop setting. Typically the script would only take a small fraction of a second to complete the task.
cron entries have six fields-:
mins hours day month day-of-week path_to_command
0-59 0-23 1-31 1-12 0-6 command
days of the week start on Sunday. 0=Sunday, 1=Monday etc.
cron entry to run the script every hour for all days and months-:
0 * * * * /path/change_wallpaper.sh
to list your current cron jobs, type
crontab -l
Edit your cron jobs and add the new cron entry-:
crontab -e
Check the new setting is in place -:
crontab -l
I would personally run the script using following crontab:
0 * * * * $HOME/changewallpaper.sh
which you can install as a user with this command
crontab -e
Other solutions include running daemon script from file ~/.xprofile
For more information please refer to
man crontab
man 5 crontab
Also check out this project Variety.
Also, how could I get this script to run in the background, so that a terminal window is not always required to be open?
That would be a daemon. And there's no need to write your own. It's a bit tedious in bash if you want pidfile, start|stop|restart etc. Just add a new cronjob which'll execute your script every n minute or something.
Edit your cronjobs
crontab -e
Execute script every 30 min: (not the same as 30, which would do it every hh:30!)
*/30 * * * * /path/to/your/script
Restart cron. How depends on distro, here's Ubuntu:
service cron restart
List cronjobs:
crontab -l
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Cron job on Ubuntu for php
I am running and ubuntu server and wanted to run a php script every day. I have done some research and found that cron is the best way of doing this however, this is where i got stuck, a lot of the information on the internet about cron is very hard to follow and understand.
So i wanted to execute a simple php script once a day, the script i made for testing simply just deletes a record from a database, but the real script will do a lot more.
I tried setting up a task through plesk which is provided through my web host service but it didn't seem to execute when i wanted it to, i used 1 for minutes, 22 for hours, * for day, * for week and * for month and thought this would execute every day at 22:01.
I have the directories on my server:
cron.hourly
cron.daily
cron.weekly
cron.monthly
I thought i could dump i file in there and it would execute for example every day, but i'm guessing i need to make a cron script to call a php script right?
If i were to go the way of putting a file in the cron.daily folder how would i go about it?
Also if there are any steps i need to take on the php side please let me know?
Thanks a lot for your time.
There's couple of ways to setup cron job. Assuming you got shell access you could do crontab -e from console and define job there, i.e. like this:
1 22 * * * command
which would trigger command (whatever it is) at 22:01 each day (not sure why you set minutes to 1 instead of 0 though). To launch PHP script from there you would either have to install php-cli, and then invoke it that way:
1 22 * * * <path>/php -q script.php
You can also call bash script here, to setup all the stuff like paths etc and then call your php script form bash - sometimes it is simpler to do that way instead of crafting way too long command line for cron. And it's simpler to update it later. also, you could turn your php script into bash-runnable script by setting it execution bit (chmod a+x script.php) and adding shell's shebang:
#!/usr/bin/php -q
<?php
...
If your script got too many dependencies and you'd prefer to call it via web, you could use wget to mimic a browser. so your command would be:
/usr/bin/wget --delete-after --quiet --spider <URL-TO-YOUR-SCRIPT>
wget manual can be accessed by man wget or wget -h, or is on this website. Aternatively you may use HEAD tool from perl-www package - but it requires perl while wget is a standalone tool. If you use HTTPS with self signed certs, add --no-check-certificate to your invocation arguments. And you may also want to setup .htaccess and limit web access to your cron script to localhost/127.0.0.1
every minute:
* * * * * /path/script.php
every 24hours (every midnight):
0 0 * * * /path/script.php
Se this reference for how crontab works: http://adminschoice.com/crontab-quick-reference, and this handy tool to build cron jobx: http://www.htmlbasix.com/crontab.shtml