How do you set up cron task? [closed] - cron

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 10 years ago.
Improve this question
I have never used CRON before but I want to use CRON in order to be able to perform schedule jobs for a php script. The php script is called "inactivesession.php" and in the php script is this code:
<?php
include('connect.php');
$createDate = mktime(0,0,0,10,25,date("Y"));
$selectedDate = date('d-m-Y', ($createDate));
$sql = "UPDATE Session SET Active = ? WHERE DATE_FORMAT(SessionDate,'%Y-%m-%d' ) <= ?";
$update = $mysqli->prepare($sql);
$update->bind_param("is", 0, $selectedDate);
$update->execute();
?>
Wht I want to do is that when the above date is reached (25th Oct), I want the php script to perform the UPDATE statement above. But my question is that how do I use CRON in order to do this?
The server I am using is the university's server known as helios, does CRON need to be set up in helios, (do I have to call the admin for this) or is it something else which uses CRON.
I have never used CRON before so can you explain to me how CRON can be set up for the example above with the server I am using?
Thanks

Method 1: Execute the script using php from the crontab
Just like how you call your shell script (As show in our crontab 15 examples article), use the php executable, and call the php script from your crontab as shown below.
To execute myscript.php every 1 hour do the following:
crontab -e
00 * * * * /usr/local/bin/php /home/john/myscript.php
Method 2: Run the php script using URL from the crontab
If your php script can be invoked using an URL, you can lynx, or curl, or wget to setup your crontab as shown below.
The following script executes the php script (every hour) by calling the URL using the lynx text browser. Lynx text browser by default opens a URL in the interactive mode. However, as shown below, the -dump option in lynx command, dumps the output of the URL to the standard output.
00 * * * * lynx -dump http://www.thegeekstuff.com/myscript.php
The following script executes the php script (every 5 minutes) by calling the URL using CURL. Curl by default displays the output in the standard output. Using the “curl -o” option, you can also dump the output of your script to a temporary file as shown below.
*/5 * * * * /usr/bin/curl -o temp.txt http://www.thegeekstuff.com/myscript.php
The following script executes the php script (every 10 minutes) by calling the URL using WGET. The -q option indicates quite mode. The “-O temp.txt” indicates that the output will be send to the temporary file.
*/10 * * * * /usr/bin/wget -q -O temp.txt http://www.thegeekstuff.com/myscript.php

Related

Can't get Crontab jobs to work at all [duplicate]

This question already has answers here:
CronJob not running
(19 answers)
Closed 4 years ago.
New to Crontab use, I am trying to get a simple bash script to run.
add_temp:
#!/bin/bash
rnd1=$RANDOM
range1=20
let "rnd1 %= $range1"
rnd2=$RANDOM
range2=50
let "rnd2 %= $range2"
echo $rnd1
echo $rnd2
cd /var/www/html
sqlite3 test.db <<EOF
INSERT INTO temps (date, temp) VALUES ($rnd1, $rnd2 );
EOF
crontab -e:
SHELL:/bin/bash
* * * * * /var/www/html/add_temp
This doesn't seem to run at all. Works fine if run manually with /var/www/html/add_temp.
My guess is that the sqlite3 is not found when the cron daemon run the script.
You could modify the script and provide the full pathname of this command. Use a terminal to display the full pathname of the command:
type sqlite3
The cron daemon which will run the script does not have the exact same environment than the bash login shell used to run manually the script.
The variable that is usually differently set is PATH.
The first step to troubleshoot this situation is to compare the environments and debug the script.
I suggest to insert these lines below, after the very first line of the script
env
set -x
Run the script manually and redirect output, from a terminal:
/var/www/html/add_temp >/var/tmp/output_m.txt 2>&1
Change the crontab line for:
* * * * * /var/www/html/add_temp >/var/tmp/output_c.txt 2>&1
Wait that the cron daemon executes the script and compare the /var/tmp/output_m.txt and /var/tmp/output_c.txt files.
When the script will be fixed, remove the 2 debug lines from the script and restore the crontab original content.

How do I change the subject of each of my cron jobs?

This my crontab file:
# Edit this file to introduce tasks to be run by cron.
MAILTO=richardheyes#gmail.com
0 0 * * * ./backup.sh
0 8,12,16,20 * * * /bin/bash /kunden/homepages/46/d548322256/htdocs/dev/admin/sitemaps/create.sh
0 8,12,16,20 * * * `which php7.1` -q /kunden/homepages/46/d548322256/htdocs/dev/admin/sitemap-html/create.php
As you can see there's a backup shell script, a shell script creates a plain text sitemap file and a PHP command-line script that creates a HTML sitemap.
The resulting sitemaps are viewable here:
https://www.rgraph.net/sitemap.txt
https://www.rgraph.net/sitemap.html
The resulting emails from the cron tasks come through to me as intended but with a subject line like this:
Cron <u78819167#infongp-uk31> `which php7.1` -q /kunden/homepages/46/d548322256/htdocs/dev/admin/sitemap-html/create.php
Cron <u78819167#infongp-uk31> ./backup.sh
Which is just plain ugly. Now I could prevent the email by ensuring the tasks don't produce any output or redirecting the output to /dev/null - but if any errors crop up I'd like to see it.
So my question is:
How can I change these butt ugly subjects to something that looks nicer, on a per cron job basis? Is there an environment variable that I can set from each script that would be used as the subject?

how to run a bash script with multiple lines on crontab [duplicate]

This question already has answers here:
cronjob does not execute a script that works fine standalone
(3 answers)
Closed 6 years ago.
Because vpnc stopped every ~23 hours, I created a .sh file that is running as a cron job every 10 minutes, all that it does, is stop the vpnc process and run it again.
I have made it executable by chmod + x ping_vpnc.sh and it works fine when I run it from the terminal via ./ping_vpnc.sh
My file looks similar to:
#!/bin/sh
killall vpnc #just to make sure I don't create too many tunnels.
vpnc default.conf #run vpnc connect file.
my crontab file:
*/10 * * * * /home/username/ping_vpnc.sh
the problem with the script that it does't run fully, so it just kill the process without re-running it.
I'm running the script as root so I don't think that it's a privilege issue.
Any idea about why this is happening? I will appreciate it.
As indicated on comments, change
*/10 * * * * /home/username/ping_vpnc.sh
for
*/10 * * * * /bin/sh /home/username/ping_vpnc.sh
that is, tell crontab which binary has to execute the script.
For future references, let me point out the question you found in Ask Ubuntu: Script doesn't run via crontab but works fine standalone. It provides comprehensive information about the topic.

How to execute a php script every day [duplicate]

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

crontab doesnt work in linux

My Linux version is red hat enterprise linux server release 5.3 tikanga
i have schedule crontab as below
1 * * * * /usr/testjob.sh 2>&1 >> /usr/result.txt
crontab job not running on scheduled time...
Please suggest..
Try this at first.
* * * * * /usr/testjob.sh
Then you may received a mail for every minutes. Check the error output.
Sometimes, it may caused by your default shell is just sh instead of bash.
So, maybe ">>" is not supported.
You should check do you have /usr permission when you want to write into it.
As said by +Shawn Chin, if you want to run your command only once, the at command is your friend.
If you want to run your command repeatedly, then you are right to use the cron framework. The manual page explaining the fields of the crontab may be obtained with the following command:
$ man -s 5 crontab
You appear to be in an Indian time-zone (IST). You may have to specify that into the crontab. For instance, using the 'crontab -e' command (to save and quit, type 'ESC-wq', as the editor is VI by default):
#
CRON_TZ=IST
# run at 06:33 (am), every day
33 06 * * * /usr/testjob.sh >> /usr/result.txt 2>&1
Note that '2>&1' should be placed AFTER '>> /usr/result.txt', not before.
just to mention it and make sure
NOTE: Each cron table entry must have a trailing line break in order
for the cron table entry to be recognized.

Resources