Kcron

From MEPIS Documentation Wiki

Jump to: navigation, search

Contents

Introduction

A daemon is a computer program that is designed to run in the background rather than be under the direct control of a user. Cron is the UNIX® system scheduler daemon which is used to schedule tasks to run automatically in the background at specified times. Cron uses a configuration file named crontab which defines the tasks and associated scheduled run times in tabular form. The format for crontab is illustrated as follows...


|<---------------- minute (0 - 59)
| |<------------- hour (0 - 23)
| | |<---------- day of month (1 - 31)
| | | |<------- month (1 - 12) OR jan,feb,mar, etc.
| | | | |<----- day of week (0 - 7) (Sunday=0 or 7) OR sun,mon,tue, etc.
| | | | | |<--- command to be executed

Manual use of crontab

Crontab can be viewed directly by the user as a text file by typing the following command into Konsole:

crontab -l

In a similar manner, it can be directly edited as a text file by typing the following command into Konsole

crontab -e

There is a handy visual crontab setup on the linuxconfig.org website.

The KDE 3 and KDE 4 Interfaces

Kcron is the KDE graphical user interface to the crontab configuration file that allows the user to view and edit both tasks and schedules in a graphical display format. Normally, you would run Kcron as yourself generating a personal crontab file, but if you run Kcron as root, then you will see all crontab files including the crontab scheduled tasks for the system and the personal crontab scheduled tasks for each user on the computer.

As pictured in Figure 1, when Kcron opens in KDE 3, the user will see a list of tasks and variables. To create a new scheduled task, first select the Tasks folder. Then click Edit > New... (or right-click and choose New..., or press Ctrl+N). A self-explanatory dialogue box opens up where you identify which program you want to run and define when it will run.

Kcron.jpg

Figure 1. KDE 3 Kcron Interface


As shown in Figure 2, a more expanded interface is provided in KDE 4 for Kcron that shows the details of both tasks and environment variables.

KDE4 Kcron.jpg

Figure 2. KDE 4 Kcron Interface


Example KDE 4 Kcron Application Using Mplayer

The following example illustrates a typical Kcron application. In this instance, the goal is to set up an automatic timed recording of an online audio stream from WEHC using cron and mplayer.

The first step is to make sure all the necessary programs are installed. Go to Synaptic and make sure mplayer, Kcron, and cron are installed. In Mepis 11, mplayer and Kcron are not installed by default so you will probably need to install them. Cron should already be installed. Although it may not be necessary, once these programs are installed, you probably should re-boot just to make sure everything is running.

Now, verify that the cron daemon is running by opening the KDE Konsole Terminal Emulator and typing in the following command.

ps aux | grep cron

You should get a response similar to the following.

root 3483 0.0 0.0 4372 972 ? Ss 14:25 0:00 /usr/sbin/cron
lan3 4608 0.0 0.0 4012 652 pts/1 S+ 15:48 0:00 grep cron

The first line indicates that cron is running. The second line is your search for cron. Now, if you don't see the first line, then cron is not running and you will need to start it. One simple answer is to type the following into the Konsole command line.

/etc/init.d/cron start

This will start the cron daemon. Once started, you shouldn't have to re-start it. If cron doesn't keep running for you, then you will need to do some research to identify an appropriate solution.

Once the necessary programs are installed, you will need to create two bash scripts that record the audio and then stop the recording, respectively. As one would expect there are many ways to do this, but for the purpose of this mplayer example, we will use the following two scripts to record the stream of a radio station. Of course, you will need to modify the script with your audio source of interest.


#!/bin/sh
#This is the recording script
#First, get the date
e=`date +"%y-%m-%d"`
#Then, run mplayer to record the stream and dump to an mp3 file.
#In this case, the mp3 file will be written to the lan3 home directory, /home/lan3/
#and will be named WEHC appended with the date for later reference.
#Note: Use -dumpstream in place of -dumpaudio if the URL is not a m3u file
mplayer -vc null -vo null -playlist http://ss1.futuremediaworld.com:8000/WEHC.m3u -dumpaudio -dumpfile WEHC_$e$1.mp3
#I inserted the following sleep command because it appears that the
#bash script will shut down before the stream loads if the sleep delay is not here.
sleep 120


As mentioned previously, the script is set up to record a specific radio station. You will need to change the audio source (the http link) to whatever source you want recorded. To create the script file, open a text editor and paste the script text above into the file. Name the file Record_Radio.sh and save it. Make it executable by right clicking on the file and selecting properties, then selecting the permissions tab, and making sure the executable check box is selected.

Now if you don't specifically halt mplayer, it will just keep dumping data onto the hard drive. To counter this, we need to create a second bash script that will kill the mplayer process when the audio stream recording is completed.


#!/bin/sh
#Kill all mplayer processes
killall -9 mplayer
#Inserted sleep command for stability.
sleep 120


Create this file in the same manner as the Record_Radio.sh file...open a text editor and paste the script text above into the file. Save the file as Kill_mplayer.sh. Make it executable by right clicking on the file and selecting properties, then selecting the permissions tab, and making sure the executable check box is selected.

Now that we have created the two bash script files, we can schedule them to run automatically with Kcron. To do this, click on the system settings icon in the task bar (a crossed screwdriver and wrench) or click on the application launch icon and select settings, system settings. Select the advanced tab and click on the task scheduler icon. This launches Kcron.

IMPORTANT: You have to enter values into a base set of environment variables or Kcron will not run the bash scripts. To do this, click on the New Variable button and enter a name and value for each of the following variables.

DISPLAY :0.0
PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
USER lan3
HOME /home/lan3
SHELL /bin/bash

Of course, your USER and HOME variables will be the name and path of your home. These variables should work for most situations. If not, then you will have to experiment. Select OK and Apply.

Now we can schedule our tasks. To do this, click on the New Task button and enter the following command line...

/home/lan3/Desktop/radio/Record_Radio.sh >/dev/null 2>&1

Of course, you will need to enter a path to where you have stored your bash script files. Mine just happened to be on the desktop in a folder called radio. Now, cron is designed to send out emails whenever it runs a task and I don't want or need that so I appended the last section of the command line as you can see with >/dev/null 2>&1. This essentially disables the email function.

This next part is intuitive. Just select the buttons for month day hour minutes or days of the week when you want the program to start recording. Select OK and Apply.

Next, click on the New Task button again and enter the following command line to kill the mplayer recording...

/home/lan3/Desktop/radio/Kill_mplayer.sh >/dev/null 2>&1

Select the time you want the recording to stop (i.e., when you want Kill_mplayer to run). Select OK and Apply and that completes the cron scheduling. Remember to leave your computer on during the time you will be recording and when the recording is completed, the result will be an mp3 file stored in your home directory. If you want to review your crontab file, type the following command into Konsole...

crontab -l

NOTE: There is an issue with mplayer that may be unique to this example. Be sure to run the mplayer record script on your source before scheduling it. If you get the notification "Couldn't resolve name for AF_INET6" error message from mplayer, you will need to edit the mplayer config file manually to correct the error. To do this, select show hidden files in either the Konqueror or Dolphin KDE file managers and edit the ~/.mplayer/config file by adding the following line...

prefer-ipv4 = yes

Example KDE 4 Kcron Application Using VLC Multimedia Player

The default media player for Mepis 11 is the VLC multimedia player. In fact, mplayer is not installed as part of the default Mepis 11 install procedure. Accordingly, this section provides an example of the use of Kcron to set up an automatic timed recording of an online audio stream from WEHC using cron and the VLC Multimedia Player.

The first step is to make sure all the necessary programs are installed. Go to Synaptic and make sure VLC, Kcron, and cron are installed. In Mepis 11, Kcron is not installed by default so you will probably need to install it. Cron and VLC should already be installed.

Now, verify that the cron daemon is running by opening the KDE Konsole Terminal Emulator and typing in the following command.

ps aux | grep cron

You should get a response similar to the following.

root 3483 0.0 0.0 4372 972 ? Ss 14:25 0:00 /usr/sbin/cron
lan3 4608 0.0 0.0 4012 652 pts/1 S+ 15:48 0:00 grep cron

The first line indicates that cron is running. The second line is your search for cron. Now, if you don't see the first line, then cron is not running and you will need to start it. One simple answer is to type the following into the Konsole command line.

/etc/init.d/cron start

This will start the cron daemon. Once started, you shouldn't have to re-start it. If cron doesn't keep running for you, then you will need to do some research to identify an appropriate solution.

Once the necessary programs are installed, you will need to create a bash script that uses VLC to record the audio. As one would expect there are many ways to do this, but for the purpose of this VLC example, we will use the following script to record the stream of the WEHC radio station with the command line version of VLC called "cvlc". Of course, you will need to modify the script with your audio source of interest.


#!/bin/sh
#Bash Script to Record WEHC for 1 hour (3600 seconds)
NOW=$(date +"%b-%d-%y")
cvlc --run-time=3600 http://ss1.futuremediaworld.com:8000/WEHC.m3u --sout "#duplicate{dst=std{access=file,mux=raw,dst=/home/lan3/WEHC-$NOW.mp3}" vlc://quit ;

A couple of things to note about the above script. First, you set the length of time VLC will record using the "--run-time=" parameter in seconds. In the case of the example script, VLC will record for 3600 seconds (i.e., one hour). Also, note that the "dst=" command sets the destination path where the audio output file will be written. In the case of the example, the destination is the home directory of lan3 and the output file will be named "WEHC-" with the recording date appended.

As mentioned previously, the script is set up to record a specific radio station. You will need to change the audio source (the http link) to whatever source you want recorded. To create the script file, open a text editor and paste the script text above into the file. Name the file something like Record_Radio.sh and save it. Make it executable by right clicking on the file and selecting properties, then selecting the permissions tab, and making sure the executable check box is selected.

Now that we have created the bash script file, we can schedule it to run automatically with Kcron. To do this, click on the system settings icon in the task bar (a crossed screwdriver and wrench) or click on the application launch icon and select settings, system settings. Select the advanced tab and click on the task scheduler icon. This launches Kcron.

IMPORTANT: You have to enter values into a base set of environment variables or Kcron will not run the bash scripts. To do this, click on the New Variable button and enter a name and value for each of the following variables.

DISPLAY :0.0
PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
USER lan3
HOME /home/lan3
SHELL /bin/bash

Of course, your USER and HOME variables will be the name and path of your home. These variables should work for most situations. If not, then you will have to experiment. Select OK and Apply.

Now we can schedule our tasks. To do this, click on the New Task button and enter the following command line...

/home/lan3/Desktop/radio/Record_Radio.sh >/dev/null 2>&1

Of course, you will need to enter a path to where you have stored your bash script files. Mine just happened to be on the desktop in a folder called radio. Now, cron is designed to send out emails whenever it runs a task and I don't want or need that so I appended the last section of the command line as you can see with >/dev/null 2>&1. This essentially disables the email function.

This next part is intuitive. Just select the buttons for month day hour minutes or days of the week when you want the program to start recording. Select OK and Apply and that completes the cron scheduling. Remember to leave your computer on during the time you will be recording and when the recording is completed, the result will be an mp3 file stored in your home directory. If you want to review your crontab file, type the following command into Konsole...

crontab -l



External Links

Personal tools
In other languages