how create cron command - linux

how can i call some url like stackoverflow.com from cron without getting return data from page?

The format for cron is as follows:
# Minute Hour Day of Month Month Day of Week Command
# (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat)
For example, a cron to run at 9am every day and connect to a site would look like this:
0 9 * * * wget --spider http://www.stackoverflow.com > /dev/null 2>&1
Run crontab -e to edit the crontab, add this line to it and save.

use this:
wget --spider http://www.stackoverflow.com > /dev/null 2>&1
This command calls the url but doesnt download the output and redirect stderr and stdout to /dev/null

Related

Add Today's Date to Cronjob URL

I'm trying to add today's date to my cronjob that's running. Though the date isn't currently appending. Am I doing something wrong here?
0 8 * * * /usr/bin/wget -O - -q -t 1 "https://www.exampleurl.com?date=`date +\%y\%m\%d`"

Crontab showing different time and Shell script when run manually shows different time stamp

#!/bin/sh
string1=$(date +"%T")
string2=$(date -r merchant.xml +"%T")
StartDate=$(date -d "$string1" +"%s")
FinalDate=$(date -d "$string2" +"%s")
echo Since, $(date -d "0 $StartDate sec - $FinalDate sec" +"%H:%M") HOURS, mail has not been updated | mail -s "Merchant File Staleness" hello#gmail.com
it is my shell script name hp.sh and output is
Since, 00:55 HOURS, mail has not been updated.
The crontab is
0 * * * * /tmp/hp.sh
and output of crontab is
Since, 07:04 HOURS, mail has not been updated.
The output of both is different. I need output of my shell script using crontab every hour.
Check the TZ variable, both system and user-defined - cron will happily oblige to whatever is set there, even if it is different from system settings.

Command runs fine in terminal, but not when run as cron job

I am trying to run the following test command every minute with cron:
apachectl status | grep 'requests currently being processed' && 'date' >> /output.txt 2>&1
This would run apachectl status and if the output contains 'requests currently being processed', it would print the result of the command date to the file output.txt.
If I run this in the terminal as the root user, there is no issue. However, running it as a cronjob every minute (I've added it to /var/spool/cron/root) as follows:
*/1 * * * * apachectl status | grep 'requests currently being processed' && 'date' >> /output.txt 2>&1
Does nothing - the output.txt file never gets generated and the job never shows in the log at /var/log/cron. What might the issue be?
You should specify the full path to the executable files, you may easily find it using which command, like which apachectl etc.
*/1 * * * * /usr/sbin/apachectl status | /bin/grep 'requests currently being processed' && 'date' >> /output.txt 2>&1

Including Cron/Crontab in my Bash Backup Script with inputs

Im doing a Bash-Backup Script with 3 Options:
Do fullbackup
Do fullbackup at a specific time with cron
First I want to ask for the path like: Path of directory: /home
Then i want the hour for the backup: Hour for the backup (0:00-23:59) : 2:00
Then a simple question like: The backup will execute at 2:00. Do you agree(y/n)
I know how to do a crontab but I have no idea how to include that in my script so that I choose option 2 and then the script asks me for the directory and time and set then the crontab.
Any ideas or help would be appreciated!
You can get the existing crontab with
crontab -l
and install a new crontab with
crontab file
So your script would probably need something like
crontab -l | grep -v "# install-backup-script" > /tmp/file.$$
echo "$min $hour * * * /full/path/to/script # install-backup-script" >> /tmp/file.$$
crontab /tmp/file.$$
rm -f /tmp/file.$$

How to setup a crontab to execute at specific time

How can I set up my crontab to execute X script at 11:59PM every day without emailing me or creating any logs?
Right now my crontab looks something like this
#daily /path/to/script.sh
When you do crontab -e, try this:
59 23 * * * /usr/sbin/myscript > /dev/null
That means: At 59 Minutes and 23 Hours on every day (*) on every month on every weekday, execute myscript.
See man crontab for some more info and examples.
Following up on svrist's answer, depending on your shell, the 2>&1 should go after > /dev/null or you will still see the output from stderr.
The following will silence both stdout and stderr:
59 23 * * * /usr/sbin/myscript > /dev/null 2>&1
The following silences stdout, but stderr will still appear (via stdout):
59 23 * * * /usr/sbin/myscript 2>&1 > /dev/null
The Advanced Bash Scripting Guide's chapter on IO redirection is a good reference--search for 2>&1 to see a couple of examples.
You will with the above response receive email with any text written to stderr. Some people redirect that away too, and make sure that the script writes a log instead.
... 2>&1 ....

Resources