Saturday, July 12, 2014

A bit of cron.

There comes a time when you want to let your computer be sort of a secretary and do jobs for you automatically at certain times, This instructable will help you do that,  Wanted  the computer to tell me when I was at 200,000 total views. The goal was to have a little popup tell when that happened. I could of been real fancy and have the computer send up a flag, turn on lights, or whatever. The pop- up was enough for me. We will be using cron to do all the work for us.

Example of one project I used cron for:

More info on cron at

Step 1: Get the info.

First we need to create a batch file to get the information from the web. We will be using  just a simple page scraping to get the total views. I have talked about page scraping before in other instructables. (i.e. and others).

So you want to use your favorite edtitor (gedit, vim, nano, or etc) and  create (or a name you prefer).

$ nano
# batch file to let me know when I get to 200,000 views.
let goal=200000
let k200=$(elinks$username/index.html | grep "Total Views" | sed 's/[^0-9]*//g')
# echo $k200
if [ $k200 -gt $goal ]; then
   zenity --info --text "Hip hip hooray, you hit 200,000 views." --display=:0.0

Then we need to make tce program execucable.

$ chmod +x

So far so good.

Step 2:

We could maually run the batch file every once in a while to see that the status is. With linux, there is a better way. We can let the computer be a secretary and do the job for us. For this we need to create an entry with crontab.

Check and see if cron is running:

$ service crond status
Redirecting to /bin/systemctl  status crond.service
crond.service - Command Scheduler
      Loaded: loaded (/lib/systemd/system/crond.service; enabled)
      Active: active (running) since Sat, 28 Jan 2012 18:57:17 -0600; 10h ago
    Main PID: 817 (crond)
      CGroup: name=systemd:/system/crond.service
           817 /usr/sbin/crond -n

Edit the cron file for your entry to run once an hour. .

$ crontab -e

*  */1  * * * /home/eddie/bin/

Look at /var/log/cron to see if the file is running.
Jan 29 05:10:06 oedt01 crontab[4613]: (eddie) BEGIN EDIT (eddie)
Jan 29 05:10:37 oedt01 crontab[4613]: (eddie) REPLACE (eddie)
Jan 29 05:10:37 oedt01 crontab[4613]: (eddie) END EDIT (eddie)
Jan 29 05:11:01 oedt01 /usr/sbin/crond[817]: (eddie) RELOAD (/var/spool/croneddie)
Jan 29 05:11:02 oedt01 /USR/SBIN/CROND[4688]: (eddie) CMD (/home/eddie/bin/


You will need to add the following to your .bashrc file if you have not already done so.
xhost local:yourusername> /dev/null

Step 3: We did it!!!

The time finally comes and you get the special message. (At the time of creating this instructable we had not reached 200,000 views, so I forced the popup for testing purposes).

Step 4: Second example.
Note with new email security, this may not work.

Let your system get your email for you and save a summary to a file. This way you can read your email at your leisure and not worry about logging in unless you want to reply to the enails.
curl -u $username:$pword --silent "" | tr -d '\n' | awk -F '<entry>' '{for (i=2; i<=NF; i++) {print $i}}' | perl -pe 's/^<title>(.*)<\/title><summary>(.*)<\/summary>.*?<name>(.*?)<\/name>.*$/\n$3\n\t$1\n\t$2/' >> currentmail

Make it executable.
$ chmod +x

Have it check for mail every hour
$ crontab -e

*  */1  * * * /home/eddie/bin/

gedit/vim/nano currentmail

    inre. project.
    Marie: We have decided to go ahead with the project. Good luck

Then you can delete if if you want.

Step 5: Get Youtube files script.

Sometimes I would like to download a set of youtube files, but I do not want to sit and wait for all the downloads. I create a text file with the video links. Then I set cron job to run a batch file that will automatically download the files at some other time say late at night when I am sleeping. (Good job for an older computer!!) When it is done, you should have a folder of the videos readdy to watch at your convenience.

Screenshot from 2013-06-27 12:52:21.png

#  Get youtube videos
# Assignments
# --------------------------------
# Data input
while read line
do theurl=$line

youtube-dl $theurl -f flv

done < $datafile

No comments:

Post a Comment