Cron tar with file filtering - linux

I need to change the following cron so that it only tars images with a pattern of: l_*.jpg
What modifications does my current cron require?
0 4 * * 1 tar vcf /home/XXXXXX/public_html/backups/monday_backup.tar /home/XXXXXX/public_html/images/products

Try something like this:
0 4 * * 1 find /home/XXXXXX/public_html/images/products -iname "l_*.jpg" | tar vcf /home/XXXXXX/public_html/backups/monday_backup.tar --files-from=-
It's really simple to create a script-file.
Just make a text file with content.
#!/bin/bash
date
echo It is a blue day
Place that file into private folder (folder not accessable by browser) on the host.
Using cpanel set executable permissions on it.
Try to run it from cron:
*/2 * * * * /home/XXXXXX/private_scripts/backup-script 2>&1 >> /home/XXXXXX/private_scripts/backup-log
Check content of /home/XXXXXX/private_scripts/backup-log. If you see messages 'It is a blue day', than cron setup and script are ok.
If you don't see anything, then try to replace '#!/bin/bash' with '#!/bin/sh'. Double check paths.
If you setup script successesfully, then add to the end of script the line:
find /home/XXXXXX/public_html/images/products -iname "l_*.jpg" | tar vcf /home/XXXXXX/public_html/backups/monday_backup.tar --files-from=-

Related

Crontab cannot execute

I tried to use crontab to execute my py file everyday, but it can only create empty log file
0 8 * * * /usr/local/bin/python3 /Users/UserName/Downloads/Crawling_1.py > /Users/UserName/Downloads/log.log
Then I tried to use SHELL file to execute a simple demand, if I put log file settings inside .sh file, no log file was created. Similarly, the crontab did not execute when I put python3 demand inside the SHELL file.
echo 1 > /Users/UserName/Downloads/new_log.log > /Users/UserName/Downloads/log.log
But if I directly run echo in Crontab, it can work out perfectly.
* * * * * echo 1 > /Users/UserName/Downloads/new_log.log
Does anyone know why this is happening? Thank you so much.
Try it with >>:
0 8 * * * /usr/local/bin/python3 /Users/UserName/Downloads/Crawling_1.py >> /Users/UserName/Downloads/log.log
then with >> it will create a file if it doesn't exist. If the file existe, it will be appended to the end of the file.
With > the whole file will replace it, if the file exist. If the file not exist*, nothing happens.
You can do the command also with >, but be sure, that the .log-file, where you will write inside, exist!

How can I move a file and append a timestamp to its name via cron job?

I have a file CBD_DATA.zip present in a directory: /home/cbd_dev/CBD_DATA.zip. I want to move it to the directory /home/sundaram_srivastava/archives/ as CBD_DATA_{DateTimeStamp}.zip.
I have tried using a cron job:
* * * * * mv /home/cbd_dev/CBD_DATA.zip /home/sundaram_srivastava/archives > /home/sundaram_srivastava/archives/CBD_DATA_`date +\%d\%m\%y`.zip
The problem with the cron job above is that it moves the file as CBD_DATA.zip into another directory with the same name and then it creates another file CBD_DATA_110620.
Now, the file CBD_DATA_110620 is 0 KB. So, in the destination directory, I have two files, CBD_DATA.zip and CBD_DATA_110620, but I want just one and it should not be empty.
What should I change in my cron code?
First, I'd try and figure out the command on its own, without a cron job. What you're doing is something like this (with shortened directory paths for readability):
mv /foo/CBD_DATA.zip /foo/archives > /foo/archives/CBD_DATA_`date +%d%m%y`.zip
This moves the file and then creates a new empty file; there is no output from the mv command, and the redirection has nothing to redirect, so the file with the datestamp is empty.
The second argument for the mv command is the new location itself; if it is a directory, the filename stays the same, but if it is not a directory, it is interpreted as the new name. You don't need any redirection.
mv /foo/CBD_DATA.zip "/foo/archives/CBD_DATA_$(date '+%d%m%y').zip"
I have replaced the deprecated backticks in the command substitution with $(...) and quoted the expansion. Side note: if you can choose the datestamp format, I'd strongly recommend using +%F (or %Y%m%d) instead so it sorts chronologically.
With your paths and escaped for a cron job (do you really want to run this every minute?):
* * * * * mv /home/cbd_dev/CBD_DATA.zip "/home/sundaram_srivastava/archives/CBD_DATA_$(date +\%d\%m\%y).zip"

how to add a timestamp along with the error log for a script from crontab

I have a crontab running like:
*/15 * * * 4,5 /apps/ins/sid/compare_stats 2>> /apps/ins/sid/compare_stats.err
Everything working as expected. the only thing is I want my error logs to generate in the compare_stats.err file like this:
Jul 3 14:45:04 <error text>
which means I just want to add a date along with this. Is there any way to do it by modifying the crontab entry ( without really making any change in my script) ?
Thanks in advance.
Use the ts command which is part of the moreutils package. E.g.:
*/15 * * * 4,5 /apps/ins/sid/compare_stats | ts '[%Y-%m-%d %H:%M:%S]' 2>> /apps/ins/sid/compare_stats.err
This will prepend the timestamp to every line of the output and save it into your log.

Use crontab job send mail, The email text turns to an attached file which named ATT00001.bin

I want to analysis some data in one linux server,then send the it as Email text to my Email account , But when i execute this shell scripts in shell command, It works well, Weird is that when i put all the procedure into crontab job, The Email text will turns to an attached file, Can someone help?
#* * * * * sh -x /opt/bin/exec.sh >> /opt/bin/mailerror 2>&1
/* exec.sh */
#/bin/sh
cd /opt/bin
./analysis.sh > test
mail -s "Today's Weather" example#example.com < test
But when i execute exec.sh in shell command line directly, The Email will get text, Can someone explain it for me, grate thanks.
Ran into the same problem myself, only I'm piping text output into mailx - Heirloom mailx 12.4 7/29/08
When running the script on the command line the email came out as normal email with a text body.
However, when I ran the exact same script via crontab the body of the email came as an attachment - ATT00001.BIN (Outlook), application/octet-stream (mutt) or "noname" (Gmail).
Took some research to figure this out, but here goes:
Problem
Mailx will, if it encounters unknown / control characters in text input, convert it into an attachment with application/octet-stream mime-type set.
From the man page:
for any file that contains formatting characters other than newlines and horizontal tabulators
So you need to remove those control characters, which can be done with i.e. tr
echo "$Output" | /usr/bin/tr -cd '\11\12\15\40-\176' | mail ...
However since I had Norwegian UTF8 characters: æøå - the list expand, and you don't really want to maintain such a list, and I need the norwegian characters.
And inspecting the attachment I found I had only \r, \n the "regular" ASCII characters in range 32-176 - all printable and 184 and 195 --> UTF8
Sollution
Explicitly set the locale in your script:
LANG="en_US.UTF8" ; export LANG
Run export in your shell - or setenv if you run csh or tcsh to determine what your locale is set to.
Explanation
Mailx - when run in your shell - with LANG set to .UTF8, will correctly identify the UTF8 chars and continue.
When run in crontab LANG is not set, and default to LANG=C, since by default crontab will run only a restricted set of environment variables (system dependant).
mailx (or other programs) will then not recognize UTF8 characters and determine that the input containes unknown control characters.
My issue was UTF8 characters, yours could be other control characters in your input. Run it through hexdump or od -c, but since it works OK in a regular shell I'm suspecting LANG issues.
References:
linux mail < file.log has Content-Type: application/octet-stream (a noname attachment in Gmail)
http://alvinalexander.com/blog/post/linux-unix/how-remove-non-printable-ascii-characters-file-unix
I had this same issue and none of the above fixed the problem. Moving the extra return in the file fixed the issue for me:
cat logfile | tr -d \\r | mailx -s'the logfile' to-me#.....
Thanks to this forum:
https://forums.opensuse.org/showthread.php/445955-mailx-creates-unwanted-attachment
Make sure you change this in your script
#/bin/sh
to be replaced by
#!/bin/sh
Coming to the problem
Your script assumes that it is being run from a particular directory (note that almost every path is a relative path, not an absolute path). cron happens to be running it from another directory.
The Fix for text appearing on email
mydir=$(dirname "$0") && cd "${mydir}" || exit 1
./opt/bin/analysis.sh > test
mail -s "Today's Weather" example#example.com < /opt/bin/test
Explanation
$0 is the (possibly relative) filename of the shell script being executed. Given a filename, the dirname command returns the directory containing the filename.
So, that line changes directories to the directory containing the script or exits with an error code if either dirname or cd fails.
OR try to have full path like
./opt/bin/analysis.sh > test
mail -s "Today's Weather" example#example.com < /opt/bin/test
Note: The same problem is discussed earlier here
FOLLOW UP:
Try to remove
sh -x /opt/bin/exec.sh >> /opt/bin/mailerror 2>&1
and instead use
sh /opt/bin/exec.sh 2>&1 >> /opt/bin/mailerror
FOLLOW UP
You have to restart cron for changes to take effect if you do not use the crontab command to edit the file.
crontab -l > oldcrontab
cp oldcrontab newcrontab
echo "$newline" >> newcrontab
crontab < newcrontab
In my case, the cron was not a shell script but a PHP script (so I couldn't put the export LANG thing):
0 9 * * * apache php /test/myscript.php | mail -s "CRON - myscript" foo#bar.com
Solution:
In order to fix the same issue (content is mailed as attachment instead of body), I add LANG=fr_FR.UTF-8 at the beginning of the cron file:
MAILTO=vme1.etc-crond-backoffice-conf
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
LANG=fr_FR.UTF-8
0 9 * * * apache php /test/myscript.php | mail -s "CRON - myscript" foo#bar.com
NB: puting LANG=fr_FR.UTF-8 in the /etc/environment file and restarting cron service worked too.
Reference:
Set LANG in crontab https://www.logikdev.com/2010/02/02/locale-settings-for-your-cron-job/

Creating a Named Cron Job

How do you create a cron job from the command line, so that it shows up with a name in gnome-schedule?
I know how to create a cron job using crontab. However, all my jobs show up with a blank name. I'd like to better document my jobs so I can easily identify them in gnome-schedule, or similar cron wrapper.
Well, just made a cronjob in Scheduler, and took a look at my crontab file, and it looked like this:
0 0 * * * ls >/dev/null 2>&1 # JOB_ID_1
Notice the JOB_ID_1 at the end.
I went into ~/.gnome/gnome-scheduler/, looked at the files there, and there was one named just 1 (as in the number "one") which had a bit of info, including the name
ver=3
title=Hello
desc=
nooutput=1
So, I made a second cronjob:
0 0 * * * ls -al >/dev/null 2>&1 # JOB_ID_2
Copied the file 1 to 2 to match the JOB_ID_2, changed the description, making the file as:
ver=3
title=This is a test
desc=
nooutput=1
Then I switched over to Gnome-Schedule, and it had added the cronjob, and had the name updated.
Follow the same steps, and you should be able to manually name any cronjob you want

Resources