Perl script executed by Cron failed - linux

I maintained a database (MySQL), I would like back up some data to the database using a perl script. To save my trouble, I would like cron to do it for me, I inserted the following using crontab
*/5 * * * * blctrl /home/blctrl/code/perl/tt01.pl
However, cron never does its job, any suggestions to get it done? The Linux installed is Centos 5?

*/5 * * * * blctrl /home/blctrl/code/perl/tt01.pl
That looks like the syntax for /etc/crontab, the system-wide crontab file. The first 5 words indicate when to run the command, the 6th is the account under which to run it, and the rest of the line is the command to execute.
(The clue was that the command is under /home/blctrl, which would be the home directory for the account blctrl.)
The syntax for your own crontab, the one you feed to the crontab command, is different. You don't specify an account name, because it only runs under your own account.
Try this:
*/5 * * * * /home/blctrl/code/perl/tt01.pl
EDIT: Incidentally, the first thing I would have tried when encountering a problem like this would be to replace the command with something simple, perhaps touch /tmp/FOO. That would have told you whether the problem was with your Perl script or with your crontab.

Related

fetching email from outlook O365 by App-wsgetmail for RT using crontab not working

I have configured the modern authentication for O365 using App::wsgetmail module for RT, everything works fine when I run the the wsgetmail --config=config1.json via terminal it can access the email's inbox and create the ticket in RT and mark the email as read but when I am trying to use the crontab to do the same work periodically getting no result.
the corntab works with other cornjobs that I have for other tasks and only it is not working with wsgetmail.
the crontab looks like this:
*/1 * * * * wsgetmail --config=/path_to_config_directory/config1.json
Note:
I am using RT4
The config1.json file is executable
I am using root user for configurations
There is no any error in the logs
Any idea about this issue that I am facing with?
I have finally resolved the issue and posting the answer here for others that may face with same issue.
The crontab and terminal are two different environments, the wsgetmail perl module command is recognizable for terminal but to make it recognizable for corntab we have to add the full path to the module (wsgetmail) in the crontab.
*/1 * * * * /usr/local/bin/wsgetmail --config=/path_to_config_directory/config1.json
or we can create a shell script and call the script in crontab.
in this case test.sh will looks like this:
#!/bin/bash
/usr/local/bin/wsgetmail --config=account01.json
/usr/local/bin/wsgetmail --config=account02.json

Running a crontab every 15 minutes not working on linux redhat

I want to run a crontab every 15minutes. I tried this:
0 */15 * * * ./usr/My_PATH/run.sh
But I get this error:
0 command not found
Is there something wrong with the syntax ?
Many thanks.
UPDATE:
I corrected the script and I tried this:
*/15 * * * * /My_Path/run.sh
and this
0,15,30,45 * * * * /My_Path/run.sh
In both cases I get an error.
#1 bash: */15: No such file or directory
#2 bash: 0,15,30,45 command not found
If this:
0 */15 * * * ./usr/My_PATH/run.sh
fails with this error:
0 command not found
then you're trying to run it as a shell command. You need to feed it to the crontab command. There are several ways to do this.
crontab -l will list the current contents of your crontab; it doesn't modify it.
crontab -e will open (a copy of) your crontab in a text editor and let you modify it. This is probably the simplest way to update it.
crontab filename reads the specified file and replaces your current crontab with its contents. (If you already have a crontab, this will quietly clobber it.)
The method I recommend is to keep a separate file containing your crontab (say, crontab.txt).
First, if you already have a non-empty crontab (check with crontab -l), save it to the file:
crontab -l > crontab.txt
Make whatever additions or other changes you want to that file, and then use
crontab crontab.txt
to install the updated crontab.
You can keep backup copies (I maintain mine in a source control system) so you can recover if you mess something up. And you can do a quick crontab -e if you want to test something, then re-run crontab crontab.txt to revert to the stored crontab.
The syntax of the crontab line in your question:
0 */15 * * * ./usr/My_PATH/run.sh
is correct, but the path ./usr/My_PATH/run.sh looks like it may be incorrect. Cron jobs run from your home directory, so the path is valid only if the usr directory is directly under your home directory (and in that case the ./ is unnecessary). It's probably better to specify the full path, which can start with $HOME/.
Yes.
First field is minutes. Second field is hours. You're setting it off at zero minutes past the hour, every 15th hour. So basically - 15:00 each day.
You want:
*/15 * * * * /some_script
Furthermore - ./ - it's a relative path, and that's probably a bad idea with cron, because it doesn't chdir to run stuff. Use an absolute path to avoid confusion. If you absolutely need to be in a particular directory for the script to work, you can try:
cd /path/to/script && ./this_script
So it's quite possible that you've got broken permissions or just not finding a relative path that you're using.

Crontab absolute path not working

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.

cronjob not executed

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.

Crontab no error but doesn't execute script

I'm trying to execute a shell script from cron on Freebsd.
To test whether crontab is working at all, I wrote the line
* * * * * echo "Hello" > /home/myuser/logile
and it work fine.
But when trying to execute any script it doesn't do anything, not even an error. (In the script I tried to run is just the same echo command)
Below is the output of crontab -l:
SHELL=/bin/sh
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
HOME=/home/myuser
MAILTO=myuser
* * * * * /home/myuser/shellscript.sh > /home/myuser/logfile
Why is the script not getting executed, although crontab is obviously running?
Permission for all files are set to rwxr-xr-x.
* * * * * /bin/sh /home/myuser/shellscript.sh
or
* * * * * /bin/bash /home/myuser/shellscript.sh
worked for me in Macosx 10.6 as rootuser
Have you checked that the command line has a linefeed/CR at the end of the line? I struggled for hours trying to find a reason for non-executing php script on cron when I simply hadn't pressed enter at the end of the line when I edited the cron jobs with crontab -e :-)
Have you checked /var/log/cron for clues?
Have you tried
* * * * * /bin/sh /home/myuser/shellscript.sh > /home/myuser/logfile
cron sends any errors via email to owner of the crontab file (often "root" so you might check that account's email). To have any errors mailed to "crontabOnFreebsd" put:
MAILTO=crontabOnFreebsd
in your crontab (near the top).
For more info issue this command:
man 5 crontab
If you are getting an error, then your logfile might not capture it, try this:
* * * * * /home/myuser/shellscript.sh > /home/myuser/logfile 2> /home/myuser/errorfile
Its been a while since I did any cron stuff; but things that always used to get me:
Environment variables not been set: generally I found it necessary to set up full paths (even to things like 'cat') to all commands [or at least set ENV variables within the script itself].
Ensure the user who owns the script etc is really the user which is running the script: this might not be the same user when you test from the interactive shell. Same goes for the directories/files that the script might write to.
Also: check the email for the root user - you might find that the errors have been diverted to the inbox, which may help you troubleshoot this further.

Resources