Crontab Centos Run Perl every 2 Minutes - linux

i have a server RHEL 7 where i must run a script "perl" in every 2 minutes , all hour , all day , all week .
so i made theses steps ,
nano /etc/crontab
and inserted this command below .
2**** projop /web/projop/packages/intranet-helpdesk/perl/./import-pop3.perl
But is not running . this is my full crontab :
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,$
# | | | | |
# * * * * * user-name command to be executed
#29 3 * * * /root/bin/export-dbs > /var/log/postgres/export-dbs.log 2>&1
#24 3 * * * /usr/bin/find /export/backup -name '*.tgz' -mtime +6 | xargs rm
#25 3 * * * /usr/bin/find /export/backup -name '*.bz2' -mtime +6 | xargs rm
2**** projop /web/projop/packages/intranet-helpdesk/perl/./import-pop3.perl
someone could help me ? thanks.

If projop is a command call to a program with +x right and referenced into the PATH or profile or bashrc, be sure about this command and try to test it.
2 * * * * projop /web/projop/packages/intranet-helpdesk/perl/./import-pop3.perl
regards

Related

Cron script doesn't work unless called by sh command

I have a script saved in file named file.sh
It is executed by command
chmod +x file.sh
file.sh
#!/bin/sh
find /var/log/*.gz -printf "%T# %Tc %p\n"|sort -n|tail -n +6|sudo xargs rm|echo"test"|wall
I am trying to execute it by cron job.
Crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
* * * * * root /etc/file.sh
Script is launched every minute by cron job, I am getting echo in terminal.
But xargs rm doest works . I do know why.
When I execute command sudo sh file.sh, is OK, and file is deleted.
Please help me find the reason why the script is not working in cron-job.
It's strange.
Thank You !
The problem is your printf statement ... I'm fairly certain that you should also see error messages from rm in root's mail to the effect that e.g.:
1634986839.0000000000 Sun 24 Oct 2021 12:00:39 AM UTC /var/log/syslog.2.gz doesn't exist.

Bad hour in crontab file

I am trying to run a scheduled job via crontab in linux mint. For that, I am using the crontab -e command to edit a crontab file with the following information:
0 50 * ? * * * sh test.sh
After which I get the error:
"/tmp/crontab.XCXmSA/crontab":22: bad hour
errors in crontab file, can't install.
I tried searching but couldn't find anything that solved the problem. Tried a bunch of different times and still nothing. Any ideas?
You use totally wrong syntax. You add more stars. And questionmark which is not accepted there. Here is the syntax you search:
50 * * * * sh test.sh
And as mentioned in comments you cant have 50 as hour definition
And instead of using explicit shell add it in shebang and make the script executable
You put 50 as an hour. Hour should be in 0..23 range.
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed

How can I create Cron Expression which works between particular times

I am working on a Cron Expression for a job that has to run on weekdays every 10 mins from 7.30 am to 8.20 am(inclusive).
The trigger used by me:-
0 30/10 7-9 ? * SUN,MON,TUE,WED,THU *
Issue is it works beyond 8.20 as well? Can we limit it to specific minutes or end time? like 7:30-8:20
Some things you cannot do in a single expression and you might consider to use two:
# Example of job definition:
# .--------------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed
# This runs on 7:30, 7:40, 7:50
30/10 7 * * 1-5 command
# This runs on 8:00, 8:10, 8:20
0-20/10 8 * * 1-5 command
Another way you could attempt this is by using a test.
# Example of job definition:
# .--------------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed
*/10 7,8 * * 1-5 testcmd && command
Where testcmd is an executable script that could look like:
#!/usr/bin/env bash
hours=$(date "+%H")
minutes=$(date "+%M")
(( hours == 7 && minutes >= 30)) || (( hours == 8 && minutes <= 20 ))
Other examples of this trick can be found here:
Cron expression to run every N minutes
How to schedule a Cron job to run 4th week of the year
how to set cronjob for 2 days?

How to set customised schedule in linux cron?

I'm in need to set a cron in crontab like, a php file should be called according to the below schedule
Mon-Fri, 9:00AM to 3:30 PM every one minute
I tried like below,
*/1 * * * * wget http://example.com/trail.php
Can someone help me how to create a cron for the above requirement?
Thanks,
Check the manpage of cron (man 5 crontab). You can do a lot of things, but there's no easy and simple way to achieve what you want. Probably the easiest is to use two entries:
* 9-14 * * * <command>
0-30 15 * * * <command>
You can use following pattern and code
Use the hash sign to prefix a comment
# +---------------- minute (0 - 59)
# | +------------- hour (0 - 23)
# | | +---------- day of month (1 - 31)
# | | | +------- month (1 - 12)
# | | | | +---- day of week (0 - 7) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed
#--------------------------------------------------------------------------
*/1 9-14 * * 1,2,3,4,5 wget http://example.com/trail.php
0-30 15 * * 1,2,3,4,5 wget http://example.com/trail.php
Finally, got the obsolete solution.
*/1 9-14 * * 1-5 wget -O /dev/null example/alerts.php
0-30 15 * * 1-5 wget -O /dev/null example/alerts.php
making this work like a charm.
Thanks for the answering, learnt from #Smit Raval and #Ulrich Eckhardt.

How can i run cron-job every 1 hour in Ubuntu 14.04?

I want to run a cron for every 1 hour.
What i tried :
0 */1 * * * /home/username/test.sh
0 * * * * /home/username/test.sh
But, i am not sure, which one is right ?
Can you please help me to decide . which one i should use?
This is the way to set cronjob on ubuntu for every 1 hour
0 * * * * or you can put it in cron.hourly directory
For more details you can refer link:
https://askubuntu.com/questions/2368/how-do-i-set-up-a-cron-job
# Use the hash sign to prefix a comment
# +---------------- minute (0 - 59)
# | +------------- hour (0 - 23)
# | | +---------- day of month (1 - 31)
# | | | +------- month (1 - 12)
# | | | | +---- day of week (0 - 7) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed
#--------------------------------------------------------------------------
* */1 * * * root /bin/sh /home/username/test.sh

Resources