Cron script doesn't work unless called by sh command - linux

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.

Related

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

CRON with AND relationship between mday and wday

I have seen this question which indicates that the relationship between the wday and mday fields of a CRON schedule is an OR relationship. Say for example I want to schedule something for every Friday the 13th.
Rather than the expected result, the CRON
0 0 13 * 5
will give me all Fridays of every month, as well as every 13th of every month.
Is there any way to avoid this behavior and specify an AND relationship? (There seems to be mention of older versions using an AND relationship, however I would prefer to use a single tool with the ability to do both)
I guess, instead of specifying the wday (Friday=5), you'll just have to specify the months where the 13th is a Friday; so, for 2019:
0 0 13 9,12 * : do stuff, but avoid black cats
Or, eternally more elegant, create a small script:
$> cat /home/me/bin/test_friday_13
#!/bin/bash
if [ "$(date +'%A %d')" != "Friday 13" ]
then
exit 1
else
: do stuff, but avoid black cats
exit 0
fi
and make the crontab entry:
0 0 13 * * /home/me/bin/test_friday_13
The script approach has the added benefit that you can run it from the command line. (Note: do not forget to alter the weekday name in the script to reflect your environment.)
The cron-entry you are interested in is:
# 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
0 0 13 * * [ $(date "+\%u") = "5" ] && command
This will execute the cronjob every 13th of the month. It will compute the day of the week, and test it if it is a Friday (5). If so, it will execute command.
Extended explanation:
If you want to have special conditions, you generally need to implement the conditions yourself with a test. Below you see the general structure:
# 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
0 0 13 * * /path/to/testcmd && command
The command testcmd generally returns exit code 0 if the test is successful or 1 if it fails. If testcmd is successful, it executes command. A few examples that use similar tricks are in the following posts:
Linux crontab for nth Satuday of the month
Is it possible to execute cronjob in every 50 hours?
Cron expression to run every N minutes
how to set cronjob for 2 days?
The test you want to perform is written in /bin/sh as:
[ $(date "+%u") = 5 ]
Which makes use of the test command (see man test). This is POSIX compliant and will work with any shell that your cron might run in (sh,bash,dash,ksh,zsh). The command date "+%u" returns the weekday from 1 to 7 (Sunday is 7). See man date for more info.
So your cronjob would look like:
0 0 13 * * [ $(date "+\%u") = 5 ] && command
Note that we have to escape the <percent>-character.
A % character in the command, unless escaped with a backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.
source: man 5 crontab
Unfortunately, CRON doesn't work the way you want
You can see all available options on below url
https://www.freeformatter.com/cron-expression-generator-quartz.html
So what you can do is the execute the cron on every 13th of the month but don't let the command run when its Friday
0 0 0 13 * ? * mybatchjob.sh
mybatchjob.sh
if [ `date +'%A'` = "Friday" ]; then
echo "Yep its Friday";
else
echo "Not Friday";
fi
This will make sure that the intended program only runs on Friday and the 13th

Schedule cronjob to copy files to different location every week

I need help to schedule a cron on my server to copy a specific files to different locations every week in a month.
Here's what I'm trying to achieve,
Week 1 - Copy to location A
Week 2 - Copy to location B
Week 3 - Copy to location C
Week 4 - Copy to location D
The job will then repeat itself every month and I'm planning to use cp -rf command to overwrite the old files.
Could someone shed some light on how can I achieve this?
There are many ways to achieve this, basically you could use many declarations in your crontab, one for each week, or a single call to a script that would decide based on the current date.
Follows an example of how to make different cron calls every week:
0 0 1 * * cp -fr /path/to/file /path/to/destination/A
0 0 8 * * cp -fr /path/to/file /path/to/destination/B
0 0 15 * * cp -fr /path/to/file /path/to/destination/C
0 0 22 * * cp -fr /path/to/file /path/to/destination/D
You may check the expected running datetime here:
https://crontab.guru/#0_0_1__
https://crontab.guru/#0_0_8__
https://crontab.guru/#0_0_15__
https://crontab.guru/#0_0_22__
The below example will run the cronjobs at 1 AM server time on the 1st, 8th, 15th and 22nd of each month.
0 1 1 * * rsync -avz /path/to/directory /path/to/destination/A
0 1 8 * * rsync -avz /path/to/directory /path/to/destination/B
0 1 15 * * rsync -avz /path/to/directory /path/to/destination/C
0 1 22 * * rsync -avz /path/to/directory /path/to/destination/D
The format for cronjobs is:
+---------------- minute (0 - 59)
| +------------- hour (0 - 23)
| | +---------- day of month (1 - 31)
| | | +------- month (1 - 12)
| | | | +---- day of week (0 - 6) (Sunday=0 or 7)
| | | | |
* * * * * command to be executed
I'd recommend using rsync instead of the cp command. If there are a lot of files involved, you can also create a ZIP or Tar before performing the copy.
Thank you!

Crontab Centos Run Perl every 2 Minutes

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

Set cron job issue

I am first time trying to set cron job in my linux server. I want to set every 10 mins my file will run.
The cron works fine but the problem is that the cron run as following
00:00 00:10 00:20
but i want it in this way
00:05 00:15 00:25
If you have Vixie cron (the most common implementation these days), you can use this syntax:
5-55/10 * * * * command
where 5-55 specifies a range of minutes and /10 says to run once every 10 minutes.
If not, just enumerate all the times you want it to run:
5,15,25,35,45,55 * * * * command
Running man 5 crontab should show you the documentation. (man crontab will show you the document for the crontab command; man 5 crontab describes the file format.)
Reading the manual page you need an entry in your crontab file such as
5,15,25,35,45,55 * * * * <script path>
http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/
Check out this link
Easy to remember format:
* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

Resources