I did the following cron task with sudo crontab -e:
0 * * * * /usr/local/bin/docker_build/backup.sh
Cron works well (launch backup to each hour).
In this bash script, I write a backup sql file based on this pattern :
DATE_NOW=$(date +%Y-%d-%m-%H-%M)
$DATE_NOW.sql
But the hour return by %H variable is not the hour of my current timezone.
I check the following commmand in my shell :
date //return good date
cat /etc/timezone //return good timezone
UPDATE :
It was a very stupid mistake. To be short, my bash script run in a docker container. The container timezone wasn't set with my current timezone. I had the following in my image build :
RUN echo America/Montreal > /etc/timezone && \
dpkg-reconfigure --frontend noninteractive tzdata
You could specify the timezone in the crontab entry
0 * * * * env TZ='Europe/Madrid' /usr/local/bin/docker_build/backup.sh
Or, I suppose, in the crontab itself
TZ='Europe/Madrid'
0 * * * * /usr/local/bin/docker_build/backup.sh
You can find out what the timezone is in root's cron environment
* * * * * date "+\%F \%T \%Z \%z" > /tmp/root.cron.date.txt
Related
I have been trying to configure a cron job to run my ansible playbook every hour. I could not find any relevant examples on how to start the configuration . I have tried the below task in a separate ansible playbook and it shows the below output for crontab -l, but not execution seems to be happening. Help will be high appreciated.
root#ubuntu:/etc/ansible/# crontab -l
57 10 * * * ansible-playbook crontest.yaml
- name: Run an ansible playbook"
cron:
name: "Run play"
minute: "57"
hour: "10"
job: "ansible-playbook crontest.yaml"
Cron Logs:
Nov 24 07:35:01 ABC30VDEF290021 CRON[13951]: (root) CMD (echo "testing" > /etc/ansible/automation/logs/test.txt)
Nov 24 07:35:01 ABC30VDEF290021 CRON[13948]: (root) CMD ( /usr/bin/ansible-playbook /etc/ansible/automation/crontest.yml)
Nov 24 07:35:01 ABC30VDEF290021 CRON[13949]: (root) CMD (/usr/local/bin/ansible-playbook /etc/ansible/automation/main.yaml)
Crontab -e
#Ansible: Run play
*/1 * * * * /bin/sh -c '. ~/.profile; /usr/local/bin/ansible-playbook /etc/ansible/automation/crontest.yml
*/1 * * * * echo "testing" > /etc/ansible/automation/logs/test.txt
35 7 * * * /usr/local/bin/ansible-playbook /etc/ansible/automation/main.yaml
The following code will run the script every 30th minute of every hour
- name: Run CRON job to load data at every 30th minute of every hour.
become: yes
become_method: sudo
cron:
name: "load_data"
user: "root"
weekday: "*"
minute: "30"
hour: "*"
job: "python3 /path/to/my/script/loadScript.py > /home/ec2-user/loadData_result 2>&1"
state: present
I'm trying to build a laravel scheduler to save my data history. I create a command "MisReportBackup". then I add it in the kernel too.
protected $commands = [
'\App\Console\Commands\MisReportBackup',
];
my schedule function is
protected function schedule(Schedule $schedule)
{
$schedule->command('MisReportBackup:MisReport')->dailyAt('16:59');
}
then i create a cron job on server
59 16 * * * cd /home/public_html/outpace-erp-v1.1 && php artisan schedule:run >> /dev/null 2>&1
it's 17:13:58(SGT) but did not work yet.
There's no need to specify the schedule for the cron job. For Laravel's scheduler to work, it has to run every minute.
* * * * php /home/public_html/outpace-erp-v1.1/artisan schedule:run >> /dev/null 2>&1
See the documentation.
* * * * * /usr/local/bin/php /home/technolive/public_html/outpace-erp-v1.1/artisan schedule:run > /dev/null 2>&1
it's work.
I was trying to schedule automated tasks using but nothing would happen
* * * * * root /usr/bin/php /home/user/laravel/artisan schedule:run >> /home/user/cron.log 2>&1
So I tried calling the job directly using the command below
* * * * * root /usr/bin/php /home/user/laravel/artisan email:panelReport >> /home/user/cron.log 2>&1
After I enter the command above I keep this error in my cron.log
No command 'app' found, but there are 16 similar ones
app: command not found
If I run php aritsan email:panelReport I recieve the email just fine.
Kernel.php
protected $commands = [
Commands\EmailPanelReport::class
];
protected function schedule(Schedule $schedule)
{
$schedule
->command('email:panelReport')
->everyMinute()
}
EmailPanelReport.php
protected $signature = 'email:panelReport';
protected $description = 'Send out weekly report';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$panels = Orders::where('orders.prefix', 'P')->get();
$columns = array ('Date', 'Order Number', 'Description');
if ( !ini_get("auto_detect_line_endings")) {
ini_set("auto_detect_line_endings", '1');
}
try
{
$csv = Writer::createFromFileObject(new SplTempFileObject());
$csv->insertOne($columns);
$csv->insertAll($panels->toArray());
$output = $csv->getContent();
Mail::raw('See attached', function($message) use ($output)
{
$message->to('test#gmail.com');
$message->subject("test");
$message->attachData($output, 'test.csv', [
'mime' => 'text/csv',
]);
});
$this->info("local");
}
catch (\Exception $ex)
{
$this->error($ex->getMessage());
Mail::raw($ex->getMessage(), function($message)
{
$message->to('myemail#me.com');
});
}
}
System info:
Ubuntu 16.04
nginx/1.10.3
php 7.1.7
Other ways I have tried
* * * * * root /usr/bin/php /home/user/laravel/artisan email:panelReport >> /home/user/cron.log 2>&1
* * * * * root /usr/bin/php usr/share/nginx/www/laravel/artisan email:panelReport >> /home/user/cron.log 2>&1
* * * * * /usr/bin/php /home/user/laravel/artisan email:panelReport >> /home/user/cron.log 2>&1
* * * * * root /usr/bin/php /home/user/laravel/artisan schedule:run >> /home/user/cron.log 2>&1
You can schedule it writing your command using the crontab editor:
crontab -e
And paste your command on end of file, exit and save. The results should looks like this:
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
To see your scheduled commands run:
crontab -l
I hope it helps!
Very simple and yet not working. Path not get set.
crontab:
* * * * * source /home/inst1/.profile; /home/inst1/Scripts/test.sh > /home/inst1/Scripts/test.log 2>&1
.profile:
[..whatever..]
PATH=/tmp
export PATH
test.sh:
#!/usr/bin/bash
echo $PATH
cat to test.log gives me:
/usr/bin:/etc:/usr/sbin:/usr/ucb:/usr/bin/X11:/sbin:/usr/java5/jre/bin:/usr/java5/bin
I guess it related to AIX environment, but I have no idea - seems simple but I'm missing something somewhere.
Try this:
* * * * * bash -c "source /home/inst1/.profile; /home/inst1/Scripts/test.sh" > /home/inst1/Scripts/test.log 2>&1
cron uses /bin/sh by default, while source is not a POSIX shell command. You need to use . instead:
* * * * * . /home/inst1/.profile; /home/inst1/Scripts/test.sh > /home/inst1/Scripts/test.log 2>&1
I have the following command in my crontab:
* * * * * root /home/amith/m.sh >dev/null 2>&1
where m.sh consists of:
#!/bin/sh
curl -0 "http://www.google.com" > /home/amith/Desktop/h2
but the command in shell script is not executing at all.
Can anyone please tell me the solution?
First be sure that your script /home/amith/m.sh is running correctly!
Your crontab entry is wrong you dont need root before script. Also your redirection to /dev/null is not good you are missing / before dev
You can set env varibale for SHELL in crontab with this
crontab -e
SHELL=/bin/sh
Then add your script:
* * * * * /home/amith/m.sh >/dev/null 2>&1
* * * * * cd /full/path; sh m.sh;
First, change the directory into your file location (use cd). Then, bash it with sh command.
Change * * * * * with the time schedule. To run the file every minute * * * * * To run every hour (1:00, 2:00, 3:00 and so on) use 00 * * * * To run everyday at 6 AM use 00 06 * * * To run the file on 9th March at 6 AM use 00 06 09 03 * The structure is minute hour date month day of week If you want to schedule it every Monday at 06 AM use this 00 06 * * 1 See this link http://www.pantz.org/software/cron/croninfo.html