Unix Cron for running job on 1st weekday - cron

Can someone help mw with the cron expression for running job on 1st weekday??
Is it possible in simple unix crontab?

If your week start on Monday and you want to run script every monday you should use something like (suppose you run your script at 22h 00m)
0 22 * * 1 /path/to/script
If you want to run the script first work day of month and your week start on Monday you can try something like:
0 22 1,2,3,4,5,6,7 * * /path/to/script
and add on the begin of the script this:
if [ "$(date +%u) -ne 1 ]
then exit
fi
this will end the script if you run it on day of week which is not Monday

Related

Using Linux Cron to Run Task every Fortnight

I have a server running Linux operating system. I am trying to schedule a cron job to run a task every two weeks (Fortnight) in Tuesday at 9am.
I can only manage to run the task manually by comment (if I don’t want to run the job for this week) and uncomment (if I want to run the job for this week) as following:
0 9 * * 2 root java -jar test.jar // will run
# 0 9 * * 2 root java -jar test.jar // will not run
I have attempted to use the following cron job:
0 9 * * 2 case $(($(date +\%s) / (60*60*24*7))) in *[02468]) root java -jar test.jar
But this cron script does not seem to work.
Any thought
I would try to execute at 4AM every one of two tuesdays :
0 4 * * 2 test $((10#$(date +\%W)\%2)) -eq 1 && execute_cmd
We first get week number with date and correct formatting, then the 'one time out of two' thing is done by using 'test' command.
'test' evaluates to true or false depending on a given expression, here, it evaluates whether $((10#$(date +\%W)\%2)) equals 1 (hence -eq 1), in other words it returns true if the week number is odd.
So this crontab will execute on odd weeks, on tuesday (2) at 4 AM (0 4).
More in-depth details here :
https://serverfault.com/questions/633264/cronjob-run-every-two-weeks-on-saturday-starting-on-this-saturday

I want the cron job to run the script weekly basis

I am trying to setup a cron job on a Ubuntu server. I want the cron job to run the script on weekly basis. Problem is - It should be a working day, If im mentioning it with time interval, it fails during weekoffs - Need an Schedular Exp which has to work weekly only on working days at office hours.(9am to 9pm)max.
Want to Execute the script every week #6 pm during the weekdays. It Can be Mon to Fri.
Step1
sudo apt-get install cron
systemctl status cron
Step2
Configure the cron job:
crontab -e
Select an editor of your choice
0 0 * * 0 /path/to/command
Syntax: minute hour day-of-month month day-of-week command.
Day-of-week goes from 0-6 where 0 is Sunday.
Save it.

Crontab not running on sunday night

So I made this so it would send a command at Sunday night at 11:59pm, 1 minute before Monday, but for some reason it doesn't work. I even changed the * * 7 to 0.
59 23 * * 7 screen -S skyblock -p 0 -X stuff "mangdelp default essentials.fly ${printf \\r)"
Your syntax is incorrect.
It should be like this:
59 23 * * 0 screen -S skyblock -p 0 -X stuff \"mangdelp default essentials.fly ${printf \\r)\" >/dev/null 2>&1
minute(s) hour(s) day(s) month(s) weekday(s) command(s)
The fields are
separated by spaces or tabs. The first five are integer patterns and
the sixth is the command to be executed. The following table briefly
describes each of the fields.
Field Value Description
minute 0-59 The exact minute that the command sequence executes
hour 0-23 The hour of the day that the command
sequence executes
day 1-31 The day of the month that the command
sequence executes
month 1-12 The month of the year that the command
sequence executes
weekday 0-6 The day of the week that the command
sequence executes. Sunday=0, Monday = 1, Tuesday = 2, and so forth.
command Special The complete sequence of commands to be executed. The
command string must conform to Bourne shell syntax. Commands,
executables (such as scripts), or combinations are acceptable.

Special crontab expression

just to verify my understanding of cron-syntax, the following expression will fire on a saturday at 02:42 in the middle of the month, right?
42 02 12-19 * 6 myScript > /dev/null 2>&1
cheers
Nicolaie
The script myScript (not good without a path here!) will be executed at 2:42 indeed, on every day between and including the 12th to the 19th of each month that is a saturday.
Actually
42 02 12-19 * 6 myScript > /dev/null 2>&1
means run on 12 thru 19 and every Saturday.
You need a more complex line to do what you state:
0 4 8-14 * * test $(date +\%u) -eq 6 && echo "2nd Saturday"
is an example from the manfile on my system. It uses a trivial execution after making sure it is Saturday.
See http://www.adminschoice.com/crontab-quick-reference/ as well as > man 5 crontab (at the commandline) for more.

Setting cron job end time

I want to set up a cron job which will execute a command every hour. However, I want that this command should be started at 10 A.M and should run every hour till 4 P.M. This job is to run daily between these times. The command is nothing but a call to a Perl script. Following crontab entry runs fine and invokes the script every hour
* */1 * * * cd path_to_file; perl file.pl > path_to_logs/log.txt
Is there a way to limit the timings of this cron job so that it runs only between 10 A.M and 4 P.M ?
man 5 crontab is your friend. (Your example does not do what you claim it does; /1 is the default skip and therefore redundant, and that spec therefore runs once per minute due to the leading * instead of 0.)
0 10-15 * * * your command here
(I used 15, because it occurs to me that "between 10 and 4" is an exclusive range so you don't want to run at 16:00.)
If you want the script to be run every hour you can do something like this:
[code]
00 10,11,12,13,14,15,16 * * * cd path_to_file; perl file.pl > path_to_logs/log.txt
[/code]
This means when the minutes hit 00 and the hour hits any of 10 11 12 13 14 15 16 the script will be run
In your Perl script (or in a wrapper for the Perl script), you can use localtime to check the hour and exit if it isn't between 10am and 4pm:
use strict;
use warnings;
my #lt=localtime;
my $hour=$lt[2];
unless($hour>=10 and $hour<=16)
{
print "Not between 10am and 4pm. Exiting.\n";
exit;
}
#put the rest of your code here.

Resources