I've tested the cronjob in local which is working fine. But it is not working in server. Any help is appreciated. Thanks in advance.
"sudo systemctl status cron" command gives following outputs:
cron.service - Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2022-07-19 21:40:44 CST; 27min ago
Docs: man:cron(8)
Main PID: 511 (cron)
Tasks: 1 (limit: 2315)
Memory: 1.9M
CGroup: /system.slice/cron.service
└─511 /usr/sbin/cron -f
Jul 19 22:05:01 iZa2d4o64ljp79h0v3i5fvZ CRON[2292]: pam_unix(cron:session): session closed for user userr
Jul 19 22:06:01 iZa2d4o64ljp79h0v3i5fvZ CRON[2313]: pam_unix(cron:session): session opened for user userr by (uid=0)
Jul 19 22:06:01 iZa2d4o64ljp79h0v3i5fvZ CRON[2314]: (userr) CMD ( cd /var/www/abc.com.np/myproject/app/Console/Kernel.php && php artisan schedule:run >> /dev/null 2>>
Jul 19 22:06:01 iZa2d4o64ljp79h0v3i5fvZ CRON[2313]: pam_unix(cron:session): session closed for user userr
Jul 19 22:07:01 iZa2d4o64ljp79h0v3i5fvZ CRON[2333]: pam_unix(cron:session): session opened for user userr by (uid=0)
Jul 19 22:07:01 iZa2d4o64ljp79h0v3i5fvZ CRON[2334]: (userr) CMD ( cd /var/www/abc.com.np/myproject/app/Console/Kernel.php && php artisan schedule:run >> /dev/null 2>>
Jul 19 22:07:01 iZa2d4o64ljp79h0v3i5fvZ CRON[2333]: pam_unix(cron:session): session closed for user userr
Jul 19 22:08:01 iZa2d4o64ljp79h0v3i5fvZ CRON[2353]: pam_unix(cron:session): session opened for user userr by (uid=0)
Jul 19 22:08:01 iZa2d4o64ljp79h0v3i5fvZ CRON[2354]: (userr) CMD ( cd /var/www/abc.com.np/myproject/app/Console/Kernel.php && php artisan schedule:run >> /dev/null 2>>
Jul 19 22:08:01 iZa2d4o64ljp79h0v3i5fvZ CRON[2353]: pam_unix(cron:session): session closed for user userr
crontab (/etc/crontab)
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
MAILTO=""
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# 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
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
* * * * * userr cd /var/www/abc.com.np/myproject/app/Console/Kernel.php && php artisan schedule:run >> /dev/null 2>&1
#
Kernel.php in laravel project
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\DB;
class Kernel extends ConsoleKernel
{
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
//z; here if I remove the comment, the error should be shown in storage/log/laravel.log. It works locally but no error is seen in server
DB::table('recent_users')->delete();
})->everyMinute();
}
}
Test: here if I've uncommented "//z" inside $schedule->call(), the error should be shown in storage/log/laravel.log if cronjob is working but it does not. It works locally but no error is seen in server
Update: I removed the cd
/var/www/abc.com.np/myproject/app/Console/Kernel.php && php artisan schedule:run >> /dev/null 2>&1
the error I got is following:
/var/www/abc.com.np/myproject/app/Console/Kernel.php: 1: cannot open ?php: No such file
Related
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!
I know there have already been a million questions about this but I feel that I have read them all. I think some of it may be my basic understanding of how crontab works as I am not an expert at Linux yet.
I want to run a ruby script daily with crontab. I have been trying to test it using 01 * * * * ruby /home/testcron.rb from crontab -e.
Just to confirm that I'm doing it right, once I'm in crontab -e, I write out my command, then "WriteOut" and then enter to save, right? My crontab looks like the below:
# 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($#
# m h dom mon
dow command
01 * * * * ruby /home/testcron.rb >> /home/testruby 2>&1
I'm pretty sure all the weird line breaks are due to me not being able to make gitbash any wider.
Things I've tried:
01 * * * * /usr/local/rvm/rubies/ruby-2.2.1/bin/ruby
/home/testcron.rb
Defining the Shell and BASH_ENV (I think correctly?) based on this
question,
Crontab not running ruby script.
Logging with this 01 * * * * ruby /home/testcron.rb >>
/home/testruby 2>&1 but the log never appears so it appears the ruby
script isn't running.
I've tried grep CRON /var/log/syslog and it gives the below
Aug 29 07:17:01 aarmora CRON[18387]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Aug 29 08:17:01 aarmora CRON[21194]: (root) CMD ( cd / && run-parts --report /
etc/cron.hourly)
Aug 29 09:17:01 aarmora CRON[23532]: (root) CMD ( cd / && run-parts --report /
etc/cron.hourly)
Aug 29 10:17:01 aarmora CRON[24551]: (root) CMD ( cd / && run-parts --report /
etc/cron.hourly)
Aug 29 11:17:01 aarmora CRON[24910]: (root) CMD ( cd / && run-parts --report /
etc/cron.hourly)
Aug 29 12:17:01 aarmora CRON[25275]: (root) CMD ( cd / && run-parts --report /
etc/cron.hourly)
Aug 29 13:17:01 aarmora CRON[25662]: (root) CMD ( cd / && run-parts --report /
etc/cron.hourly)
Aug 29 14:17:01 aarmora CRON[25940]: (root) CMD ( cd / && run-parts --report /
etc/cron.hourly)
Aug 29 15:17:01 aarmora CRON[26114]: (root) CMD ( cd / && run-parts --report /
etc/cron.hourly)
Aug 29 16:17:01 aarmora CRON[26517]: (root) CMD ( cd / && run-parts --report /
etc/cron.hourly)
Aug 29 17:17:01 aarmora CRON[26917]: (root) CMD ( cd / && run-parts --report /
etc/cron.hourly)
Aug 29 18:17:01 aarmora CRON[27368]: (root) CMD ( cd / && run-parts --report /
etc/cron.hourly)
What am I doing wrong? How can I debug this better?
In my cron tab file, I have
* * * * * /Users/ajgauravdeep/test.sh
which looks like
1 #!/bin/sh
2
3 /bin/echo "Downloading builds"
4 #~luna/bin/mountebuild
5
6 #sleep 10
7
8 ##############---------Variables---------##############
9
10 fileWithBuildPath="/tmp/process.tmp.file.txt"
11 skihillDir="xyz"
12 lastBuild=`/bin/cat $fileWithBuildPath`
13 curBuild=`/usr/bin/readlink -n $skihillDir/x`
14
15 ##############---------Variables---------##############
16
17 /bin/echo lastbuild is $lastBuild
18
19 if [ "$curBuild" != "$lastBuild" ]; then
20 lastBuild=$curBuild
21 /bin/echo We have a new build :$curBuild
22 /bin/rm $fileWithBuildPath
23 /bin/echo "$lastBuild" > $fileWithBuildPath
24 fi
I don't see any output coming every minute on screen but when I have
* * * * * /Users/ajgauravdeep/test.sh > <some file>
I see that file is populated. Can anyone help?
Jobs run by cron are not connected to any terminal, much less your current terminal. You can't expect to a job with cron to write to a terminal.
I want to write a cron task to record the ntpdate synchronization info into the system log, but there's no such info printed in the /var/log/messages after this cron task is done, where did I do wrong?
The followings are what my crontab looks like.
*/1 * * * * ntpdate 192.168.100.97 | logger -t "NTP"
*/1 * * * * echo "log test" | logger -t "TEST"
*/1 * * * * whoami | logger -t "WHO"
When I do tailf /var/log/messages and wait some time I only got the following lines, the NTP lines are missing.
Oct 29 15:22:01 localhost TEST: log test
Oct 29 15:22:01 localhost WHO: root
Oct 29 15:23:01 localhost TEST: log test
Oct 29 15:23:01 localhost WHO: root
Oct 29 15:24:01 localhost TEST: log test
Oct 29 15:24:01 localhost WHO: root
Oct 29 15:25:01 localhost TEST: log test
Oct 29 15:25:01 localhost WHO: root
Oct 29 15:26:01 localhost TEST: log test
Oct 29 15:26:01 localhost WHO: root
But when I do the ntpdate 192.168.100.97 | logger -t "NTP" in the command line, I could see there's message Oct 29 15:28:39 localhost NTP: 29 Oct 15:28:39 ntpdate[11101]: adjust time server 192.168.100.97 offset 0.000043 sec print out in the system log. What am I missing here?
Thanks in advance for your kind help.
Edit: Solution found via Barmar's answer. Added full smartctl command path and it works via crontab now.
I have the below script:
#!/bin/bash
#set -x
EMAIL="admin#domain.co.uk"
FILE="/root/scripts/hddreport.txt"
HOST=`hostname`
HDD01="/dev/sda"
P=`ping -c 1 $HOST | sed '1 ! d' | awk '{print $3}'`
cd /root/scripts/
echo -en "HDD health check on the server hosting" $HOST $P > $FILE
echo -e "\n" >> $FILE
smartctl -H $HDD01 >> $FILE
# The above commands do correctly write the content to $FILE (proved by removing the rm command at the bottom and doing cat on the file after)
smartctl -H $HDD01
echo "\nEmailed you the health of the Hard Drive $HDD01\n"
mailx -s "HDD health check complete on `date`" $EMAIL < $FILE
rm $FILE
which runs fine by doing bash /root/scripts/diskhealth.sh as it shows this in my mailbox:
HDD health check on the server hosting domain.co.uk (0.0.0.0)
smartctl 5.40 2010-07-12 r3124 [x86_64-unknown-linux-gnu] (local build)
Copyright (C) 2002-10 by Bruce Allen, http://smartmontools.sourceforge.net
SMART Health Status: OK
But when I let it run via crontab using any of the following syntax:
X 20 * * * /bin/bash /root/scripts/diskhealth.sh
X 20 * * * /bin/sh /root/scripts/diskhealth.sh
X 20 * * * /root/scripts/diskhealth.sh
it puts everything but the smartctl disk check:
HDD health check on the server hosting domain.co.uk (0.0.0.0)
Here's what it shows if I add extra echo lines:
This is a test
HDD health check on the server hosting domain.co.uk (0.0.0.0)
Amended script for "This is a test" below:
#!/bin/bash
#set -x
EMAIL="admin#domain.co.uk"
FILE="/root/scripts/hddreport.txt"
HOST=`hostname`
HDD01="/dev/sda"
P=`ping -c 1 $HOST | sed '1 ! d' | awk '{print $3}'`
cd /root/scripts/
echo "This is a test" > $FILE
echo -en "HDD health check on the server hosting" $HOST $P >> $FILE
echo -e "\n" >> $FILE
smartctl -H $HDD01 >> $FILE
smartctl -H $HDD01
echo "\nEmailed you the health of the Hard Drive $HDD01\n"
mailx -s "HDD health check complete on `date`" $EMAIL < $FILE
rm $FILE
Here is the /var/log/syslog output from cron:
Jun 6 20:25:01 hostname /USR/SBIN/CRON[1018112]: (root) CMD (bash /root/scripts/diskhealth.sh)
Jun 6 20:25:01 hostname postfix/pickup[1016576]: 5740356613F: uid=0 from=<root>
Jun 6 20:25:01 hostname postfix/cleanup[1018125]: 5740356613F: message-id=<20130606192501.5740356613F#hostname>
Jun 6 20:25:01 hostname postfix/qmgr[292015]: 5740356613F: from=<root#hostname>, size=465, nrcpt=1 (queue active)
Jun 6 20:25:01 hostname postfix/pickup[1016576]: 631F156613E: uid=0 from=<root>
Jun 6 20:25:01 hostname postfix/cleanup[1018125]: 631F156613E: message-id=<20130606192501.631F156613E#hostname>
Jun 6 20:25:01 hostname postfix/qmgr[292015]: 631F156613E: from=<root#hostname>, size=759, nrcpt=1 (queue active)
Jun 6 20:25:01 hostname pvemailforward[1018132]: forward mail to <root#localhost.localdomain>
Jun 6 20:25:01 hostname postfix/pickup[1016576]: B597B566148: uid=65534 from=<nobody>
Jun 6 20:25:01 hostname postfix/cleanup[1018125]: B597B566148: message-id=<20130606192501.631F156613E#hostname>
Jun 6 20:25:01 hostname postfix/local[1018131]: 631F156613E: to=<root#hostname>, orig_to=<root>, relay=local, delay=0.39, delays=0.16/0/0/0.23, dsn=2.0.0, status=sent (delivered to command: /usr/bin/pvemailforward)
Jun 6 20:25:01 hostname postfix/qmgr[292015]: 631F156613E: removed
Jun 6 20:25:01 hostname postfix/qmgr[292015]: B597B566148: from=<nobody#hostname>, size=963, nrcpt=1 (queue active)
Jun 6 20:25:01 hostname postfix/smtp[1018135]: B597B566148: to=<root#localhost.localdomain>, relay=none, delay=0.16, delays=0.12/0/0.04/0, dsn=5.4.4, status=bounced (Host or domain name not found. Name service error for name=localhost.localdomain type=A: Host not found)
Jun 6 20:25:01 hostname postfix/qmgr[292015]: B597B566148: removed
Jun 6 20:25:01 hostname postfix/cleanup[1018125]: D6570566147: message-id=<20130606192501.D6570566147#hostname>
Jun 6 20:25:01 hostname postfix/smtp[1018130]: 5740356613F: to=<admin#domain.co.uk>, relay=ASPMX.L.GOOGLE.COM[173.194.67.27]:25, delay=0.68, delays=0.12/0/0.19/0.36, dsn=2.0.0, status=sent (250 2.0.0 OK 1370546701 iy4si8635735wic.1 - gsmtp)
Jun 6 20:25:01 ds9453 postfix/qmgr[292015]: 5740356613F: removed
The email is received, just missing the smartctl output.
Cron jobs don't run your .profile. So if smartctl is in a directory you add to $PATH in your profile, it won't be found when you run via cron. Try using the full pathname to the command.