Executing multiple scripts via Crontab; Continuously repeats and doesnt stop - cron

I have the following cronjob set to execute daily at a specific time:
MAILTO="email address"
16 * * * python3 /home/cladkins/NBA.py && python3 /home/cladkins/NCAABB.py && python3 /home/cladkins/NCAAFB.py
added this via sudo nano crontab,crontab -e
It is executing almost to well, this is a case of it doesn't know what to do if you don't tell it what to do. My intention is at 1600 UTC everyday to have these scripts run once. Current behavior is that it just keeps looping forever. How can i get it to execute each script only once?

I suggest you use lib like celery to manage and track the execution of your cron jobs easily.
After setting up the celery, you will end up having something like this:
CELERYBEAT_SCHEDULE = {
# Executes every day at 16:00
'my-sample-task': {
'task': 'tasks.task_name',
'schedule': crontab(hour=16, minute=0),
'args': (16, 16),
},
}
Check this link for more details.

Related

Unable to output script results with column/table formatting

Answered - previously titled 'Cron job for shell script not running'
I recently downloaded Speedtest onto my Raspberry Pi, and wrote a script to output the results in csv format to a CSV file.
I'm trying to do this regularly via a cron job, but for some reason, it won't execute the shell script as intended.
Here's the script below. I've commented/cut out a lot to try and find the issue
#!/bin/bash
# Commented out if statement detects presence of data file and creates one if it doesn't exist. Was going to adjust later to include variables/input options if I wanted to used script on alternate systems, but commented out while working on main issue.
file='/home/User/Documents/speedtestdata.csv'
# have tried this with and without quotes, does not seem to make a difference either way
#HEADERS='/usr/bin/speedtest-cli --csv-header'
SPEEDTEST='/usr/bin/speedtest-cli --csv'
# Used absolute path for the executable
#LOG=/home/User/scripts/testreclog.txt
#DATE=$( date )
# Was using the above to log steps of script running successfully with timestamp, commented out
#if [ ! -f $file ]
#then
# echo "Creating results file">>$LOG
# touch $file
# $HEADERS > $file
#fi
#echo "Running speedtest">>$LOG
$SPEEDTEST >> $file
#echo "Formatting results">>$LOG
#column -s, -t < $file
# this step was used to format the log file neatly
#echo "Time completed ",$DATE>>$LOG
And here's how the crontab currently looks
# 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
*/5 * * * * /bin/bash /home/User/scripts/testandrec.sh
# 2> /home/User/scripts/testrecerror.txt
# Was attempting to log errors to this file, nothing seen so commented out on a newline.
#* * * * * /home/User/scripts/testscript.sh test to verify cron works (it does)
I've added my scripts folder to the end of my path, but for some reason this only shows up when I'm using the Pi directly, when I ssh in I'm missing the scripts folder on the end.
However, given that I've used absolute path for everything I'm not sure why this would be an issue.
First I tested whether a simple Cron job would work, so I created testscript.sh, which simply returned 'Test' and a timestamp to a specific file and used the same shebang, and used the absolute paths, and functioned as intended.
I have checked systemctl for Cron, restarted Cron with sudo service cron restart and made sure a new line is in place in the crontab.
I have tried with and without /bin/bash in the cron tab entry, it seemingly hasn't made a difference.
I tried cd /home/User/scripts && ./testandrec.sh but no luck.
I changed the run time to every 5 then every 10 minutes, which has not worked.
I have noticed that when I ran the script manually with column -s, -t < $file left in, when cating the results file it is formatted as intended.
However, the next instance of when the cron job should run reverts this to CSV with a , as a delimitter, so clearly something is running.
To confuse matters further, I think the script may be firing once after restarting cron, and then not working when it should be running subsequently. When I leave the column line in, this appears to just revert the formatting, but if I comment it out it appears to run a speed test and append the results, but only once. However, I may be wrong on this and reproducing it
If I instead try 0 * * * * /usr/bin/speedtest-cli --csv >> /home/User/Documents/speedtestdata.csv && column -s, -t < /home/User/Documents/speedtestdata.csv, it appeared to perform/append speedtest but does not action the column command.
I would much rather neatly tie up the process in a shell script, however, rather than have the above which isn't very DRY code.
I've looked extensively, but none of the solutions I've found on this site or others have fixed the issue.
Any troubleshooting suggestions/help would be greatly appreciated.
Here you go - the solution is simple:
#!/bin/bash
# Commented out if statement detects presence of data file and creates one if it doesn't exist. Was going to adjust later to include variables/input options if I wanted to used script on alternate systems, but commented out while working on main issue.
file='/home/User/Documents/speedtestdata.csv'
# have tried this with and without quotes, does not seem to make a difference either way
#HEADERS='/usr/bin/speedtest-cli --csv-header'
SPEEDTEST='/usr/bin/speedtest-cli --csv'
# Used absolute path for the executable
#LOG=/home/User/scripts/testreclog.txt
#DATE=$( date )
# Was using the above to log steps of script running successfully with timestamp, commented out
#if [ ! -f $file ]
#then
# echo "Creating results file">>$LOG
# touch $file
# $HEADERS > $file
#fi
#echo "Running speedtest">>$LOG
$SPEEDTEST | column -s, -t >> $file
Just check the last line ;)

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).

how to specify multi time in cron expression

Is it possible to have one cron expression to run at 6am and 7:30pm every day?. I understand the following will will run 6am, 6:30am, 7:00pm and 7:30pm,
0,30 6,19 * * *
How will I schedule 6am and 7:30pm in one cron?
You can put more intelligence into the command that gets run, something like:
0,30 6,19 * * * t=T$(date +%H%M) ; [ ${t} = T0600 -o ${t} = T1930 ] && payload
This will actually run the crontab command four times a day (at 6am, 6:30am, 7pm and 7:30pm) but execute the payload proper only at 6am and 7:30pm as desired.
Some people opt to put this extra intelligence into a script that gets run and simply exits if the time isn't one of the desired ones, but I tend to prefer keeping all scheduling control in the crontab file itself.

Scheduler doesn't work in vTiger

I am an intern on a company in Malta. The company has just made a big change from sugarCRM to vTigerCRM. Now we have a problem with the scheduler. What we want is, when a mail is entered it should automatically get synced with the organisations and contacts (I can link them when I click on the "SCAN NOW" button of the mailconverter). But I want it automatically.
But my cron files are not getting updated.
I installed a cron on the linux server with the code below:
*/15 * * * * sh /vtiger_root/cron/vtigercron.sh >/dev/null 2>&1
I adapt code the PHP_SAPI and I added the permissions on the proper files. But still. (as we speak my schedule task for the mail is at 1)
So every 15 minutes the vtigercron.sh is supposed to run vtigercron.php. But it doesn't happen. When I run vtigercron manually every things works fine. (The scheduler cron states get updated) but not with the cron file on the server.
Can somebody please be my hero?
In our crontab -
(sudo) vim /etc/crontab
I scheduled the job like this:
*/15 * * * * webdaemon /bin/bash /var/www/vtigercrm6/cron/vtigercron.sh
Are you getting any errors in /var/log/syslog or /var/log/messages or whichever system log that your OS uses?
This tutorial actually works: https://www.easycron.com/cron-job-tutorials/how-to-set-up-cron-job-for-vtiger-crm
A simpler way:
In the file vtigercron.php, change the line
if(vtigercron_detect_run_in_cli() || (isset($_SESSION["authenticated_user_id"]) && isset($_SESSION["app_unique_key"]) && $_SESSION["app_unique_key"] == $application_unique_key)){
to
if(vtigercron_detect_run_in_cli() || ($_REQUEST["app_unique_key"] == $application_unique_key) || (isset($_SESSION["authenticated_user_id"]) && isset($_SESSION["app_unique_key"]) && $_SESSION["app_unique_key"] == $application_unique_key)){
and then use
http://www.example.com/vtigercron.php?app_unique_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
as cron job URL.
IMPORTANT NOTICE: You may find your app_unique_key in config.inc.php (look for $application_unique_key in it).
Please replace xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx in the URL above with the 32-chars $application_unique_key you find in config.inc.php, and www.example.com with your vtiger install location.

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