Cron job in a different timezone - cron

Is there a way of setting up a cronjob for a specific timezone?
My shared hosting is in USA (Virginia) and I am in UK. If I set a cron job to be executed at 1600 hrs every friday, then it will execute when its 1600 in Virginia.
I was wondering if I can setup my cronjob in such a way that it understands which timezone to pick. I am not too worried about daylight saving difference.
I have asked my shared hosting providers about it and they said I should be able to set the timezone in some cron ini files, but I could not find any.

I think that you should check
/etc/default/cron
or just type
Crontab cronfile
and you should find
TZ=UTC
This should be changed (for example America/New_York). Second way is set in cron example
5 2 3 * * TZ="America/New_York" /do/command > /dev/null 2>&1

You can use the CRON_TZ environment variable, excerpt from man 5 crontab on a CentOS 6 server:
The CRON_TZ specifies the time zone specific for the cron table.
User type into the chosen table times in the time of the specified
time zone. The time into log is taken from local time zone, where is
the daemon running.
So if you add this at the top of your cron entry:
CRON_TZ=Europe/London
You should be good.

If you want to set different timezone for your particular cronjob, execute crontab -evia ssh and add
TZ=Europe/Moscow
before every cronjob.
Full article here

To expand on AlexT's answer:
CRON_TZ="Europe/London"
is the answer.
p.s. If anyone is here looking for timezone fixes for GoDaddy, I recommend putting...
export TZ="Europe/London"
...in your .bashrc so that your console runs in your timezone.

On Ubuntu 18.08
host#machine$crontab -e
then enter
TZ=Asia/Kolkata
Note:- Replace Asia/Kolkata with your desire timezone

Related

Autorun script for any activity on linux server

so i have around 500 servers for which i wanted to create a script that automatically login and do some trivial activity to create some logs (basically want to send logs through siem tool to check if log sending is working or not) and then automatically logoff from the server.
I am planning that the script can be auto-run on the server every 15 days.
trivial activity can be anything(just want to create logs).
Any help how to achieve that??
EDIT
i was thinking now that stopping and starting a service in server will accomplish my need. Any help on that script. i am actually new in working in linux server. so any help is greatly appriciated.
This can be done by cronjobs, you can simply setup a cronjob to do the task,
0 0 15 1-12 * /path/to/your/script
This Cronjob will run at 00:00 on day-of-month 15 in every month from January through December.
You could use cron jobs.
In the crontab file of every server you need to make an entry of the time when you wanna run the script in your case every 15 days and the location of your script file.
0 0 */15 * * /path/to/script.sh
Mind you the job would run a bit differently, as it would start at the 1st of every month, then on 16th and then finally on 31st if the month has 31 days.

Change cron entry with Ansible without changing time

I have a bunch of systems, with a Cron-scheduled job on them, which was set up by Ansible. Checking the crontab, I can see
#Ansible: Job Name
0 22 * * * /path/to/script.sh >/var/log/folder/script.log
I need to change that entry to point to a different script. This is easy enough, except that some of the systems in question have had the timing changed, and I need to preserve that changed timing. I tried an ansible task without the timing:
- name: Update Cron
cron:
name: Job Name
job: /path/to/new/script.sh >/var/log/folder/script.log
But that of course just left the following, since each time option defaults to '*':
#Ansible: Job Name
* * * * * /path/to/new/script.sh >/var/log/folder/script.log
Is there any good way to get Ansible to update the cron entry without clobbering the current timing, or possibly a clean way to read it up and tell Ansible to write out the current time? Or am I going to have to fall back on something ugly and probably-not-recommended like using the lineinfile module to edit the appropriate /var/spool/cron/ files directly?
This will not be possible only with the cron module. But I see a way to do it cleanly and to get a chance to enhance your inventory at the same time.
use the command module with crontab -l.
parse the stdout_lines to find the cron name comment, the definition is on the next line
parse the cron definition to extract the schedule. Store this in var(s)
optionally update you host_vars in your inventory with those values so you have the schedule for a next run if needed
run the cron module with the parsed schedule.
I admit that's a lot of steps for something that is normally done with a single task but this is the only clean way I could think of.
Assuming some sort of logical grouping of your hosts, rather than parsing each host for the crontab entry, can you setup the times per-group then setup the groups in the inventory to set the times using the "cron:" module?
If you need to ensure that there is some randomness to the time entries (so you don't have 500 hosts all trying to upload a log file at the same second), you can use the "random" filter along with a seed as shown in this StackOverflow answer:
https://stackoverflow.com/a/45946949/187426

How to set half hour and 45-minute time zones without worrying about DST changes in Linux?

Currently I am setting time zone GMT+5 on my Linux CentOS 7 machine by linking /etc/localtime to /usr/share/zoneinfo/Etc/GMT-5.
I want to create a zone info file for example /usr/share/zoneinfo/Etc/GMT-5:30 that enables me to set the current timezone to GMT+5:30 without having to worry about DST changes.
I am aware that I could link /etc/localtime to /usr/share/zoneinfo/Asia/Kolkata for example but then I will have to worry about DST changes even if it may not be implemented in the mean time.
Also I am aware of TZ=GMT+5:30; export $TZ but I am worried that this approach backfire for some reason in the future.
If you don't want to use $TZ variable, you may want to create your own timezone file.
You need create a text file (say MyZone.zic) with
Zone MyZone 5:30 - GMT-5:30
then run
zic -d <desired_folder> MyZone.zic
then link compiled file to /etc/localtime

TZ (timezone) doesn't work in "/etc/crontab"

I've tried setting the timezone as:
export TZ=Europe/Paris
and as:
TZ=Europe/Paris
But none of them work.
The server is setup for UTC time. And it needs to remain that way.
My job needs to happen at 4:00am (Paris time) when the server it not being used. However it happens at 6:00am (because UTC time).
How can I fix this?
As this is flagged with CentOS, I assume that you use cron from CentOS:
https://www.unix.com/man-page/centos/5/crontab/
Use the CRON_TZ variable before a section of rules that you want to run scheduled based on a different time zone.
If you want the commands themselves use that same TZ, then you need to add that to the rules manually.

Define Silex Cron job

How to add a cron job in a Silex PHP Server?
I would like to do some tasks every midnights without a previous user request. I know we can use middleware functions in order to execute some tasks after and before a Request, but I would like to do without one of them.
I just follow some samples which use ConsoleServiceProvice but, although code doesn't show any error, the execute method is never call. And, it is not a cron task.
So, Is it possible to define a Cron job in Silex 1.x??
Thanks.
If you want manage your crontask withiout crotab from console
you can use this Cron Task MAnager
github.com/MUlt1mate/cron-manager
Simple install with composer
You can define a console command in silex, but you'll need a task scheduler to actually run in when you want. Cron is perfect for this!
If you want it to run every midnight, type crontab -e in a terminal and put this in the file:
0 0 * * * <command you want to run>
For more details on crontab formatting, see man crontab.
This is the simplest way, there are more tips and tricks to using cron, do read up on the subject!

Resources