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?
Related
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
SHELL=/bin/bash
...
30 05 * * * sh script.sh
or
SHELL=/bin/bash
...
30 05 * * * script.sh
is enough ?
It's a simple question, I'm writing a bash script called from cron grouping files in tar file and classifing into a dir structure.
These dir needs a special owner and permissions, and I call mkdir command thru su:
#!/bin/bash
... # shortened code
$PERMS=750
$DIR=/home/luser/0/01/012/0123
$OWNER=luser
... # shortened code
su -c "mkdir -m $PERMS -p $DIR" $OWNER
Output for ll -R /home/luser/0
/home/luser/0:
total 4
drwxr-xr-x 3 luser luser 4096 Jan 7 18:13 01
/home/luser/0/01:
total 4
drwxr-xr-x 3 luser luser 4096 Jan 7 18:13 012
/home/luser/0/01/012:
total 4
drwxr-x--- 2 luser luser 4096 Jan 7 18:13 0123
/home/luser/0/01/012/0123:
total 0
Only the deepest dir has permissions (750) setting rightly.
I don't know how deep it's the last directory and set permissions for all home's file it's too hard (too much files).
PS: I'm googled about that, but I find nothing.
You can restrict the permissions on the parent directories via umask. Here is an example:
PERMS=750
UMASK=$(echo "$PERMS" | tr "01234567" "76543210")
DIR=/home/luser/0/01/012/0123
OWNER=luser
su -c "umask $UMASK; mkdir -m $PERMS -p $DIR" $OWNER
In action:
> PERMS=750
> UMASK=$(echo "$PERMS" | tr "01234567" "76543210")
> (umask $UMASK; mkdir -m $PERMS -p 1/2/3/4)
> ll -R .
.:
drwxr-x--- 3 luser luser 4096 Jan 7 1:38 1/
./1:
drwxr-x--- 3 luser luser 4096 Jan 7 1:38 2/
./1/2:
drwxr-x--- 3 luser luser 4096 Jan 7 1:38 3/
./1/2/3:
drwxr-x--- 2 luser luser 4096 Jan 7 1:38 4/
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.