Re: [Cronie-devel] Contribution for Crond
by Marcela Mašláňová
On 02/17/2014 05:48 AM, Marc Perkel wrote:
> Hi,
>
> I just wanted to contribute these 2 scripts to the crond project. they
> explain themselves. on allows you to run a script up to 2400 times a
> minute. The other allows you to run a script every X minutes and X can
> be > 60 and not divisible by 60.
>
> Feel free to include these.
>
> http://wiki.junkemailfilter.com/index.php/How_to_run_a_Linux_script_every...
>
>
> #! /bin/sh
>
> # Microsecond Cron
> # Usage: cron-ms start
> # Copyright 2014 by Marc Perkel
> # docs at
> http://wiki.junkemailfilter.com/index.php/How_to_run_a_Linux_script_every..."
>
> # Free to use with attribution
>
> basedir=/etc/cron-ms
>
> if [ $# -eq 0 ]
> then
> echo
> echo "cron-ms by Marc Perkel"
> echo
> echo "This program is used to run all programs in a directory in
> parallel every X times per minute."
> echo "Think of this program as cron with microseconds resolution."
> echo
> echo "Usage: cron-ms start"
> echo
> echo "The scheduling is done by creating directories with the number
> of"
> echo "executions per minute as part of the directory name."
> echo
> echo "Examples:"
> echo " /etc/cron-ms/7 # Executes everything in that directory
> 7 times a minute"
> echo " /etc/cron-ms/30 # Executes everything in that directory
> 30 times a minute"
> echo " /etc/cron-ms/600 # Executes everything in that directory
> 10 times a second"
> echo " /etc/cron-ms/2400 # Executes everything in that directory
> 40 times a second"
> echo
> exit
> fi
>
> # If "start" is passed as a parameter then run all the loops in parallel
> # The number of the directory is the number of executions per minute
> # Since cron isn't accurate we need to start at top of next minute
>
> if [ $1 = start ]
> then
> for dir in $basedir/* ; do
> $0 ${dir##*/} 60000000 &
> done
> exit
> fi
>
> # Loops per minute and the next interval are passed on the command line
> with each loop
>
> loops=$1
> next_interval=$2
>
> # Sleeps until a specific part of a minute with microsecond resolution.
> 60000000 is full minute
>
> usleep $(( $next_interval - 10#$(date +%S%N) / 1000 ))
>
> # Run all the programs in the directory in parallel
>
> for program in $basedir/$loops/* ; do
> if [ -x $program ]
> then
> $program &> /dev/null &
> fi
> done
>
> # Calculate next_interval
>
> next_interval=$(($next_interval % 60000000 + (60000000 / $loops) ))
>
> # If minute is not up - call self recursively
>
> if [ $next_interval -lt $(( 60000000 / $loops * $loops)) ]
> then
> . $0 $loops $next_interval &
> fi
>
> # Otherwise we're done
>
>
> ##########################
>
> #! /bin/sh
>
> # Minute Cron
> # Usage: cron-min start
> # Copyright 2014 by Marc Perkel
> # docs
> athttp://wiki.junkemailfilter.com/index.php/How_to_run_a_Linux_script_eve..."
>
> # Free to use with attribution
>
> # Run this script under Cron once a minute
>
> basedir=/etc/cron-min
>
> if [ $# -gt 0 ]
> then
> echo
> echo "cron-min by Marc Perkel"
> echo
> echo "This program is used to run all programs in a directory in
> parallel every X minutes."
> echo
> echo "Usage: cron-min"
> echo
> echo "The scheduling is done by creating directories with the number
> of minutes as part of the"
> echo "directory name. The minutes do not have to evenly divide into
> 60 or be less than 60."
> echo
> echo "Examples:"
> echo " /etc/cron-min/1 # Executes everything in that directory
> every 1 minute"
> echo " /etc/cron-min/5 # Executes everything in that directory
> every 5 minutes"
> echo " /etc/cron-min/13 # Executes everything in that directory
> every 13 minutes"
> echo " /etc/cron-min/75 # Executes everything in that directory
> every 75 minutes"
> echo
> exit
> fi
>
> for dir in $basedir/* ; do
> minutes=${dir##*/}
> if [ $(( ($(date +%s) / 60) % $minutes )) -eq 0 ]
> then
> for program in $basedir/$minutes/* ; do
> if [ -x $program ]
> then
> $program &> /dev/null &
> fi
> done
> fi
> done
>
>
>
>
Hi Marc,
according to Fedora conventions it should go into doc. Interested users
can easily change it. If other members of cronie list agree, I can add
it into next release.
Marcela
10 years, 2 months
Support for different subject in cron mails
by Nikhil
Hi,
Currently, the crond (or cronie) has support for MAILTO variable to have
the cron send the messages to a different email address than that of the
user. I noticed that cron messages have weird subject form that of
username, hostname and (lengthy) command line used. I took a look at the
cronie source and it sounded fairly trivial for adding support for
configurable subject strings to the code base.
So in the cronjobs, users can have MAIL_SUBJECT as the environment variable
along with MAILTO environment variable that allows to modify the cron
email's subject.
So, here is the patch I think works for me, but please let me know if there
is anything else that needs to be checked in before having the patch
committed.
thanks,
Nikhil
--- a/src/do_command.c
+++ b/src/do_command.c
@@ -88,7 +88,7 @@ void do_command(entry * e, user * u) {
static int child_process(entry * e, char **jobenv) {
int stdin_pipe[2], stdout_pipe[2];
- char *input_data, *usernm, *mailto, *mailfrom;
+ char *input_data, *usernm, *mailto, *mailfrom, *mail_subject;
int children = 0;
pid_t pid = getpid();
struct sigaction sa;
@@ -125,6 +125,7 @@ static int child_process(entry * e, char **jobenv) {
*/
usernm = e->pwd->pw_name;
mailto = env_get("MAILTO", jobenv);
+ mail_subject = env_get("MAIL_SUBJECT", jobenv);
mailfrom = env_get("MAILFROM", e->envp);
/* create some pipes to talk to our future child
@@ -437,8 +438,15 @@ static int child_process(entry * e, char **jobenv) {
fprintf(mail, "From: \"(Cron Daemon)\"
<%s>\n", mailfrom);
fprintf(mail, "To: %s\n", mailto);
- fprintf(mail, "Subject: Cron <%s@%s> %s\n",
- usernm, first_word(hostname, "."),
e->cmd);
+ /* MAIL_SUBJECT is set but if it is empty?
*/
+ if (mail_subject && *mail_subject) {
+ fprintf(mail, "Subject: %s\n",
mail_subject);
+ } else {
+ fprintf(mail, "Subject: Cron <%s@%s>
%s\n",
+ usernm,
+ first_word(hostname, "."),
+ e->cmd);
+ }
10 years, 2 months