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.
Related
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.
This question already has an answer here:
Jquery element.text() error "is not a function" when I use elements array
(1 answer)
Closed 4 years ago.
I am trying to create a method to change my desktop background randomly. I am using crontab to handle the change every 10 minutes.
The crontab
*/10 * * * * /usr/bin/feh --recursive --randomize --bg-fill
/home/aaron/Pictures/wallpapers/minimalist 2>&1
The syslog
syslog:Oct 20 09:20:01 skull-nuc CRON[19895]: (aaron) CMD (/usr/bin/feh --recursive --randomize --bg-fill /home/aaron/Pictures/wallpapers/minimalist 2>&1)
syslog:Oct 20 09:30:01 skull-nuc CRON[20449]: (aaron) CMD (/usr/bin/feh --recursive --randomize --bg-fill /home/aaron/Pictures/wallpapers/minimalist 2>&1)
Trouble shooting -
First I changed my shell to sh and tested the command. It works. I tested the command in bash. It works. I allow it to run from cron and nothing happens and no error is produced. It just runs every ten minutes and my background only changes when I do it manually.
I have verified
Script works alone
Script works from sh
cron service is running
cron is running the command with no discernable output
I am unsure what else to do
The cron environment will usually differ from the environment you have in an interactive shell. In this case, you should check the DISPLAY environment variable, which many X utilities use to figure out which session to connect to.
If it's not set, feh will probably fail in just the way you described.
Missing environment variables can be set directly in the command line you're using in the crontab, or you can write a wrapper script that sets up the environment, then calls feh, and then call the wrapper from cron.
I have a script to backup my database at /home/<user>/bin/dbbackup. The script is executable by all users, and owned by me. The files /etc/cron.allow and /etc/cron.deny do not exist.
In my crontab I have the following lines (including a new blank line after the last line of code):
#reboot /home/<user>/.dropbox-dist/dropboxd
30 2 * * * bash /home/<user>/bin/dbbackup
However, cron is not running my dbbackup script. When I run a manual test of the script it works. When I run this test on the command line: * * * * * /bin/echo "cron works" >> ~/file I get the following error:
No command 'dbbackup' found, did you mean:
Command 'dvbackup' from package 'dvbackup' (universe)
Command 'tdbbackup' from package 'tdb-tools' (main)
dbbackup: command not found
My server is running Ubuntu Trusty. Any help please?
As the comments noted, it appears that amiga_os needed remove the reference to bash in the line.
30 2 * * * bash /home/<user>/bin/dbbackup
Should be.
30 2 * * * /home/<user>/bin/dbbackup
I usually just call scripts from their path and use "#!/bin/bash" (or wherever your bash lives) as the first line of the script. It appears the amiga_os had already done this, which is good. I don't like putting sentences into cron because it makes me nervous.
I think it was a path issue as cron executes as the user but does not read the bash profile and therefore does not work exactly like it would under your shell as it might not have access to your $PATH.
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
I have a problem running a cronjob. No experience with it, so probably overseeing something nooby. The following script works like a charm (all old filters are deleted from db) when run from the shell:
dude#linux:~> /usr/bin/env /home/dude/RubyOnRails/myproject/script/rails runner /home/dude/RubyOnRails/myproject/script/delete_old_filters.rb
I made the script executable with chmod. Now I want this to run regularly using a cronjob:
dude#linux:~> crontab -e
This file was empty, and I placed this on a single line:
* * * * * /usr/bin/env /home/dude/RubyOnRails/myproject/script/rails runner /home/dude/RubyOnRails/myproject/script/delete_old_filters.rb
I expect the script to run every minute, but nothing happens. In /etc/cron.deny, only 'guest' is mentioned, and /etc/allow does not exist. Restarting my system did not help as well.
The crontab seems to be updated proberly:
dude#linux:~> crontab -l
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.f9Et2M installed on Thu May 3 14:04:47 2012)
# (Cronie version 4.2)
* * * * * /usr/bin/env /home/dude/RubyOnRails/myproject/script/rails runner /home/dude/RubyOnRails/myproject/script/delete_old_filters.rb
But I expected here a bit as well:
dude#linux:~> atq
dude#linux:~>
The cronjob does something. Every minute an entry is added to /var/log/cron.log:
2012-05-03T15:27:01+02:00 linux /USR/SBIN/CRON[5276]: (dude) CMD (/usr/bin/env /home/dude/RubyOnRails/myproject/script/rails runner /home/dude/RubyOnRails/myproject/script/delete_old_filters.rb)
The problem is, that the job is not executed. It should remove some records from the database, but it doesn't. Running the same script manually does the trick.
Anyone seeing the (perhaps trivial) thing that I missed?
Perhaps this will help, using the brackets.
* * * * * ( colon separated commands-to-execute )
Check it.