Ubuntu crontab script not working - linux

I have 2 crontab commands.
The cron below works fine:
00 19 * * * cp -a /home/kmportal/VirtualBox\ VMs/cyn.in-disk1.vmdk /media/windowsshare
The cron below does not work after I add the time behind:
05 09 * * * cp -a /home/kmportal/VirtualBox\ VMs/cyn.in-disk1.vmdk /media/windowsshare/'date +%Y%m%d%H%M%S'cyn.in-disk1.vmdk
Please guide me to understand the command and how to solve.

You should enclose the date command with ` instead of ' and escape the % characters
Try this:
05 09 * * * cp -a /home/kmportal/VirtualBox\ VMs/cyn.in-disk1.vmdk /media/windowsshare/`date +\%Y\%m\%d\%H\%M\%S`cyn.in-disk1.vmdk

Related

Crontab doesn't execute the .sh files

I recently discover crontab on my raspberry pi and I wanted to try it so I made different lignes for execute simples programs:
$
0 21 * * * /home/pi/Desktop/Programmation/Batch/msg.sh
30 21 * * * /home/pi/Desktop/Programmation/Batch/msg2.sh
0 22 * * * /home/pi/Desktop/Programmation/Batch/msg3.sh
15 22 * * * /home/pi/Desktop/Programmation/Batch/msg4.sh
30 22 * * * /home/pi/Desktop/Programmation/Batch/msg5.sh
35 22 * * * /home/pi/Desktop/Programmation/Batch/msg6.sh
(I know: it's not batch but sh !!)
Here is the .sh program (there are all the sames):
#!/bin/bash
echo -e "Hello World !"
sleep 1000000000000000000
But that's doesn't work !!
I look on internet and I tried to change the TZ of crontab, I tried the command timedatectl to verify mi date settings :
Local time: sam. 2020-10-10 18:23:37 CEST
Universal time: sam. 2020-10-10 16:23:37 UTC
RTC time: n/a
Time zone: Europe/Paris (CEST, +0200)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
And That doesn't work... So please help me !! Thank you very much !!!

Cronjobs not sending any e-mails

I have some cronjobs written on several servers using CentOS7 in combination with Plesk 18.
When I check my cron log I see that the cronjobs are started.
Even when I manually start the scripts I want to use they work, but for some reason when the crontab does it, nothing is happening.
For instance some of the cronjobs I have are:
MAILTO=support#shoptrader.nl
0 0 * * * sh /var/www/vhosts/upgrade14.shoptrader.com/httpdocs/remove_files.sh
0 6 * * * sh /var/www/vhosts/upgrade14.shoptrader.com/httpdocs/security_htaccess/security_htaccess.sh
15 7 * * * sh /var/www/vhosts/upgrade14.shoptrader.com/httpdocs/backup_personal_files.sh
30 9 * * * sh /var/www/vhosts/upgrade14.shoptrader.com/httpdocs/find_hacks/find_hacks.sh
*/5 * * * * sh /var/www/vhosts/upgrade14.shoptrader.com/httpdocs/find_sql_injection/find_sql_injection.sh
15 0 * * * sh /var/www/vhosts/upgrade14.shoptrader.com/httpdocs/backup_templates_data/backup_templates_data.sh
If I check my log I see something like: Jan 14 09:30:01 web14 CROND[4402]: (root) CMD (sh /var/www/vhosts/upgrade14.shoptrader.com/httpdocs/find_sql_injection/find_sql_injection.sh)
I've notified myself with e-mails and get this response
/var/www/vhosts/upgrade14.shoptrader.com/httpdocs/find_sql_injection/find_sql_injection.sh: line 12: php: command not found.
Which doesn't make sense, because PHP is working on the server.

Using crontab in linux to display a message everytime you open terminal?

I want to display message in terminal:
"Welcome!"
when someone opens the terminal
To print a message when opening a terminal (or logging in via ssh) you should use the message of the day (/etc/motd).
You could use cron to overwrite this file at specific times, and so users will see the current message when they log in:
00 1 * * * "/bin/echo 'Good morning' > /etc/motd"
00 12 * * * "/bin/echo 'Good afternoon' > /etc/motd"
00 17 * * * "/bin/echo 'Good evening' > /etc/motd"

run cron every 105 minutes

I'm not great with Linux, my apologies if this is easy.
I need to run 7 different scripts via cron at 15 minute intervals. I have worked out that each script needs to run every 105 minutes (every 1 hour & 45 minutes) as follows :
Script 1 Script 2 Script 3 Script 4 Script 5 Script 6 Script 7
00:15 00:30 00:45 01:00 01:15 01:30 01:45
02:00 02:15 02:30 02:45 03:00 03:15 03:30
etc...
Now I know I can't just put this:
*/105 * * * * something
Because the minutes will only go up to 59.
Other than writing each one in a line separately, is there an easy way to do this?
Any assistance would be greatly appreciated.
No, cron can't do that directly.
What you can do is have a cron job that runs every 15 minutes, invoking a wrapper script. The wrapper script can query the current time and decide which of your 7 scripts to execute.
Doing the math is left as an exercise. Don't assume that your wrapper will start exactly at the top of the minute; there can be some delay between the time a cron job is scheduled to execute and the time it actually runs, depending on system load.
at is a good candidate for this problem.
Write a wrapper (or build into your script):
#!/bin/bash
at -f $0 now + 105 minutes
<program>
Then use at -f <script> <time> for each instance.
Or generate the crontab entries using a script (my first draft was a bit too long to be a oneliner):
#!/usr/bin/perl -Ws
use strict;
use DateTime;
our ($period, $offset, $command);
my ($offh, $offm) = split(/:/, $offset || '0:00');
my %crontab = ();
for (my $i = 0; $i < 1440; $i += $period) {
my ($h,$m) = split /:/, 'DateTime'->today->set('hour', $offh, 'minute', $offm)->add('minutes', $i)->hms;
$crontab{$m}{$h}++;
}
printf("%02d %-15s * * *\t%s\n", $_, join(',', sort keys %{$crontab{$_}}), $command) for sort keys %crontab;
Usage:
$ ./gencrontab -period=105 -offset=1:30 -command=myprogram
00 05,12,19 * * * myprogram
15 00,03,10,17 * * * myprogram
30 01,08,15,22 * * * myprogram
45 06,13,20 * * * myprogram

sh with crontab is not working in Centos

I am testing an sh script with crontab which simply creates a file
#!/bin/bash
PATH=$PATH:/bin:/usr/bin
TMP_FILE=/home/hmm/AppFlatForRent/i_am_running
touch "$TMP_FILE"
Now I want to run it on crontab and have used the following but failed
18 10 * * * sh /home/hmm/AppFlatForRent/hello.sh
Also this one
18 10 * * * usr/bin/sh /home/hmm/AppFlatForRent/hello.sh
I am also trying this
23 12 * * * /usr/bin/sh /home/hmm/AppFlatForRent/hello.sh
23 12 * * * sh /home/hmm/AppFlatForRent/hello1.sh
23 12 * * * /home/hmm/AppFlatForRent/hello2.sh
Anyone knows whats the problem?
Solution:
23 12 * * * sh /home/hmm/AppFlatForRent/hello1.sh
This works!
Give the full path to the shell from cron starting with '/':
18 10 * * * /usr/bin/sh /home/hmm/AppFlatForRent/hello.sh
Or just leave the shell out and run the script directly if it is executable:
18 10 * * * /home/hmm/AppFlatForRent/hello.sh

Resources