Crontab cannot execute - linux

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!

Related

Cronjob to add datestamp to file not running

Good day everyone.
I have an issue, and Googling the issue has not helped me, basically I have the following requirement.
cronjob that runs 1st script, output is written to a file
file that is created, to have a date stamp
2nd script executes, mail the generated file as an attachment
The issue is with adding the timestamp, if I set the cron to run and just create a file with a generic filename the cronjob runs fine.
I have tried the following:
0 8-17/1 * * * python /usr/local/bin/script1.py >> /usr/local/bin/file_`date +\%Y-%m-%d`.txt 2>&1 && python /usr/local/bin/email_script.py
0 8-17/1 * * * python /usr/local/bin/acme_transcoding_check.py >> /usr/local/bin/file_$(date +"%Y-%m-%d").txt 2>&1 && python /usr/local/bin/email_script.py
Server is running Ubuntu 16.04
You need to escape the percent-sign (%) with a backslash as explained in this answer (not mine).

Searching for end removing crontab jobs

I want to check to see if the line exists with a script:
*/2 * * * * ls -1 /var/www/html/shared-media > /var/www/html/files.txt
I also need a way to remove that SPECIFIC line in the same script.

Shell script / Python3 file on crontab

I try to run the schedule.sh (shell script) in the crontab
This is the code in the script
#!/bin/sh -x
SHELL=/bin/bash
PATH=/home/peteryph/bin:/home/peteryph/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
python3 main_schedule.py
echo "Finish"
In the crontab terminal (crontab -e)
* * * * * /home/peteryph/Desktop/working/city_google/schedule.sh > /home/peteryph/Desktop/working/city_google/echo2.txt
* * * * * /usr/bin/python3 /home/peteryph/Desktop/working/city_google /schedule.py > /home/peteryph/Desktop/working/city_google/echo.txt
In the cron status
(peteryph) CMD (/home/peteryph/Desktop/working/city_google/schedule.sh > /home/peteryph/Desktop/working/city_google/echo2.txt)
(peteryph) CMD (/usr/bin/python3 /home/peteryph/Desktop/working/city_google /schedule.py > /home/peteryph/Desktop/working/city_google/echo.txt)
In the main_schedule, the code looks like this
(I did import the files. They are a lot of them, so I did not put them right here.)
print("Please wait.........\n")
infor = update()
infor.clear_previous_value() #update the data in the DB
start_time = time.time()
infor.select_update() #insert the data into DB
print("Running time --%s seonds--" %(time.time() - start_time) + "\n")
sys.exit()
I try to execute both python file and script at the same time, but I only receive the echo from the python file. But the data did not insert into the Database on both python file and script on the crontab.
However, the script works when I execute it manually.
(I do have non-English character in my file, would that effect the crontab ?)
(Or does the path of the database has to be absolute path ? )
I found where the PROBLEMS are.
First, you have to use absolute path for your database when you call the database for sqlite3.
Second, make sure your shell script is executable :P

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.

Cron tar with file filtering

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=-

Resources