PCLinuxOS-Forums
News: ...FLASH!!! ...New PCLinuxOS Testing board now open. Register today! Be an active contributor to the PCLinuxOS future! ... Read all about it now, on THIS forum!!!..
 
*
Welcome, Guest. Please login or register. May 25, 2012, 09:11:57 PM


Login with username, password and session length


Pages: [1]   Go Down
  Print  
Author Topic: Scheduling jobs with cron. Example: your computer as a grandfather clock.  (Read 2067 times)
Texstar
Administrator
Super Villain
*****
Offline Offline

Posts: 11805



« on: July 16, 2009, 08:19:41 PM »

Original Poster: Gagarin Gambit

With cron you can configure your linux installation to perform various jobs at specific times and dates. This feature is usually used by servers, but even the casual user can take advantage of it to automate some routine work or add some nice effects. Here I'll illustrate how you can take advantage of it by a simple example: making your computer ring the hours like a grandfather clock.

Cron is included in any fresh PCLinuxOS installation, however it's only available to the root user. So the first thing you need to do is allow your user to access it. There are two ways to do this:
First you can open the terminal as super user and type
# echo [your user name] > /etc/crontab.allow
Repeat if you want to add more users.
The other way is to open konqueror as super user, create a text file named "cron.allow" in /etc, and then type the name of your user in the file (that's what the above command does). If you want to include multiple users, use a separate line for each one.

Then you have to create a crontab file for your user and input your commands. Note that each user has his/her own file. Again, you can do this through the terminal or through a text editor.

Terminal
The commands for cron are these:
crontab -l: view your current crontab
crontab -r: delete your crontab
crontab -e: edit your crontab
crontab [filepath]: replace your crontab with the specified file
To create your crontab file you have to use the crontab -e command. This will take you to a vi editor, with which it's likely you're not familiar. This page illustrates how to use vi; for basic usage you need to know that [insert] or i enters edit mode, [escape] exits it, :w saves your changes, :q quits. Any changes you did will be saved in a text file named after your user in /var/spool/cron.
Text editing
You can create this file yourself if you open konqueror as super user. It must be named like your user and located in the path mentioned above. This way you can edit your crontab in KWrite and avoid the vi editor - but in this case you should be careful not to make any mistakes, because KWrite will not point them out as the vi editor would. Alternatively you can create the file in your home directory and use the command crontab [filepath].

All right, but how does cron work?
The syntax has this format:
minute[0-59] hour[0-23] day of month[1-31] month [1-12] day of week [1-7] command
You can specify multiple values by seperating them with a comma. If you don't want to specify a value for a field, you can enter an asterisk.
Example:
01 20 1,15 * * ktorrent - will open KTorrent at 8:01 pm at the 1st and 15th of every month
0 4 * * 7 ktorrent - will open KTorrent at 4 am every Sunday.
If both the day of month and the day of week is specified, the command will be executed when either of the events happen.

Of course it's pointless to use crontab to open or quit ktorrent (unless perhaps you want to seed at specific times). Let's see then how we can make the computer sound like a grandfather clock.
I have uploaded some sounds for you at <a href="http://rapidshare.com/files/206183089/Clocksounds.zip" target="_blank">rapidshare[/url] to get you started. Supposing you extract them in your home folder, you need to input these lines in your crontab file (using either crontab -e or a text editor, whatever suits you):
0 1,13 * * * aplay /home/[user name]/1oclock.wav
0 2,14 * * * aplay /home/[user name]/2oclock.wav
0 3,15 * * * aplay /home/[user name]3oclock.wav
0 4,16 * * * aplay /home/[user name]/4oclock.wav
0 5,17 * * * aplay /home/[user name]/5oclock.wav
0 6,18 * * * aplay /home/[user name]/6oclock.wav
0 7,19 * * * aplay /home/[user name]/7oclock.wav
0 8,20 * * * aplay /home/[user name]/8oclock.wav
0 9,21 * * * aplay /home/[user name]/9oclock.wav
0 10,22 * * * aplay /home/[user name]/10oclock.wav
0 11,23 * * * aplay /home/[user name]/11oclock.wav
0 12,0 * * * aplay /home/[user name]/12oclock.wav
30 * * * * aplay /home/[user name]/1oclock.wav
As you can understand, your computer will play the specified sounds at the specific times. For example the second line means that the computer will play the 2oclock.wav sound in your home folder every day at 2:00 and 14:00 (which consists of two bell sounds), while the last line means it will play 1oclock.wav (one bell sound) at every half hour.
You'd better reboot to make sure that your changes will take effect.
If you're interested in this specific effect, I found some very nice samples from known clocks around the world here, which you can modify with a program like Audacity. You can also try a program like festival to announce the time.

And a few hints for writing your own commands.
Don't use commands that drain the resources of your system, for this can cause much frustration.
Test your command in the terminal first, to make sure it works as you want it to.
Don't use any command that might interfere with your pclinuxos installation, unless you're really sure what you're doing.

Of course this is only a very simple application of crontab. Note that you can also use it to execute a script instead of a terminal command, if you want to do more complicated things. For example you can write (or find on the net) a python script executing a backup of your important files weekly, or another to automatically download the latest photos from your favorite site. With a little skill and creativity, the possibilities are endless.

There are many pages on the internet teaching how to use cron in detail; if you want to know more, I recommend this:
http://unixgeeks.org/security/newbie/unix/cron-1.html

Note that personally I only recently learned how to use cron, so any comments or corrections from more experienced users are welcome.




Edit: by O-P to add back link to clock tones at rapidshare.
Logged

Follow the development of PCLinuxOS on Twitter
Help fund the PCLinuxOS project!

"I'm not so good on advice, can I interest you in a sarcastic reply?"
Village Idiot
Hero Member
*****
Offline Offline

Posts: 2355


Have A Nice Day.


« Reply #1 on: December 06, 2009, 04:49:14 AM »

Quote
# echo [your user name] > /etc/crontab.allow

The file should be:
Quote
# echo [your user name] > /etc/cron.allow

as it states in man crontab

Also, you can find some useful wav files to play around with if you have KDE. Look in:
/usr/share/sounds/

Logged

$ fortune
No Microsoft products were used in any way for the creation of this message.
If you are using a Microsoft product to view it, BEWARE! - I'm not
responsible for any harm you might encounter as a result.
The Chief
Hero Member
*****
Offline Offline

Posts: 1582



« Reply #2 on: December 06, 2009, 11:43:57 AM »

Tex:

While trying to get the sounds, I got:
Quote
This file can only be downloaded by becoming a Premium member

Of course,it might only be because of this:
Quote
There are no more download slots available for free users right now. If you don't want to become a premium member, you might want to try again later.

1st encounter with RapidShare, so I don't know.
Logged


Retired Senior Chief, Retired Software Engineer, Active GrandPa
Gagarin Gambit
Full Member
***
Offline Offline

Posts: 161


Space technology


« Reply #3 on: December 06, 2009, 01:00:24 PM »

TheChief, that's how RapidShare works. You can download without an account, but only one file at a time with a 15 minute interval between downloads and occasionally you'll face this error. Still it's a useful and free site to upload and share data. If you try a little later you should be able to download it.

Thanks for the correction F.Luent. I wonder how it wasn't pointed out before.

BTW, thanks Texstar for reposting this. It's being some time since I wrote it and last used cron, so even I may find it useful Shocked
Logged

Linux. Space technology.
Village Idiot
Hero Member
*****
Offline Offline

Posts: 2355


Have A Nice Day.


« Reply #4 on: December 06, 2009, 01:18:26 PM »


Thanks for the correction F.Luent. I wonder how it wasn't pointed out before.



Prolly cos anyone else used the 2nd way to create this file. Or both ways until it worked!  Grin

I saw that there as an anomaly in the filename of the two methods and wondered why. So I started hacking...

Some users might prefer to run a script and find experimenting with a new script that is located in your home directory may not run.

In that case my crontab line uses goes the full path:
Quote
0 6,18 * * * /home/fluent/somescript.sh

And don't forget to make the script executable:
Quote
chmod +x /home/fluent/somescript.sh

Happy days. Oh and thanks for the tute!
Logged

$ fortune
No Microsoft products were used in any way for the creation of this message.
If you are using a Microsoft product to view it, BEWARE! - I'm not
responsible for any harm you might encounter as a result.
The Chief
Hero Member
*****
Offline Offline

Posts: 1582



« Reply #5 on: December 07, 2009, 12:28:32 PM »

TheChief, that's how RapidShare works.

Guess I'll just keep trying until I manage to get them.  All I really need is the one ding file and the two ding file.  I was planning on trying to create a ships clock (the 8 bell thing) using cron.   Cheesy
Logged


Retired Senior Chief, Retired Software Engineer, Active GrandPa
pags
Hero Member
*****
Offline Offline

Posts: 1951


Keep it clean.


« Reply #6 on: December 07, 2009, 12:46:10 PM »

Quote
First you can open the terminal as super user and type
# echo [your user name] > /etc/crontab.allow
Repeat if you want to add more users.

When adding additional users this way, you would want to append, otherwise you'll over-write the previous contents (user(s))... Wink
Code:
# echo [your user name] >> /etc/cron.allow

Note the ">>" in stead of the single ">".
Logged
Village Idiot
Hero Member
*****
Offline Offline

Posts: 2355


Have A Nice Day.


« Reply #7 on: December 07, 2009, 02:22:53 PM »

@pags: Yep, good call.  Smiley

@TheChief:
Yeah, I was thinking yesterday that the grandfather clock example could be used with a single gong soundfile controlled by a single script run every half hour that works out what time it is and plays the gong for that appropriate amount of times.

Then I thought the script should instead contain an 'if' statements for every half hour which would allow the user to turn of the gongs during the night.

I got thinking(it must be December)  and wondered if anyone knows what difference it makes to the system efficiency if it has 48 half-hourly cron entries or just one cron entry pointing to a script with 48 different 'if' blocks. The propeller hat is on and spinning.  Wink

Logged

$ fortune
No Microsoft products were used in any way for the creation of this message.
If you are using a Microsoft product to view it, BEWARE! - I'm not
responsible for any harm you might encounter as a result.
The Chief
Hero Member
*****
Offline Offline

Posts: 1582



« Reply #8 on: December 07, 2009, 02:42:50 PM »

@F.Luent:  Don't know about bash, but it has been my general experience that separate statements are more efficient than a string of if-then-else statements, but that is all from compiled languages, where the compiler may be smarter than I am...

My other concern is the "pause" between the double dings...

For those that have never been aboard ship:

12:30 => ding

 1:00 => ding ding

 1:30 => ding ding <pause> ding

 2:00 => ding ding <pause> ding ding

and so on, up to 8 dings, and then it starts over... 

Doesn't take much to amuse sailors  -  Grin  Grin  Grin
Logged


Retired Senior Chief, Retired Software Engineer, Active GrandPa
Village Idiot
Hero Member
*****
Offline Offline

Posts: 2355


Have A Nice Day.


« Reply #9 on: December 07, 2009, 02:53:40 PM »

Quote
Doesn't take much to amuse sailors


http://en.wikipedia.org/wiki/Ship%27s_bell

 Grin
Logged

$ fortune
No Microsoft products were used in any way for the creation of this message.
If you are using a Microsoft product to view it, BEWARE! - I'm not
responsible for any harm you might encounter as a result.
The Chief
Hero Member
*****
Offline Offline

Posts: 1582



« Reply #10 on: December 07, 2009, 05:12:06 PM »

And the ships cook is responsible for the care and maintenance (mostly just polishing) of the ships bell - traditionally, anyway.
Logged


Retired Senior Chief, Retired Software Engineer, Active GrandPa
Bald Brick
PCLinuxOS Tester
Hero Member
*******
Offline Offline

Posts: 5145


I'm going South


« Reply #11 on: December 07, 2009, 05:40:25 PM »

In some languages the bells are "glasses". "Six bells" in English would be "sex glas" in Swedish. Why? Because that was the amount of rhum you'd imbibed since the beginning of you watch? No, because the bell was struck every time a half-hour hourglass was turned.
Logged

If it ain't broke
hit harder!

AMD Athlon 7450 Dual-Core Processor, 7.80 GiB RAM, Nvidia GeForce GT 120/PCIe/SSE2, OpenGL/ES-version: 3.3 0 NVIDIA 295.40, SBx00 Azalia (Intel HDA) soundcard, ‎Logitech B500 webcam, SAA7146 DVB card, HDDs: Seagate 250824AS, Western Digital WD10EAVS-00D
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines

Valid XHTML 1.0! Valid CSS! Dilber MC Theme by HarzeM