Gossamer Forum
Home : General : Internet Technologies :

Create cronjob using shell code

Quote Reply
Create cronjob using shell code
Im trying to set a cronjob using shell code but nothing seems to happen...can anyone see my booboo?

Code:
#!/bin/sh

echo "Adding cronjob..."

cat >cron.txt <<EOF
*/15 * * * * /path/to/something.cgi
EOF

cat >cron.c <<EOF
#include <stdlib.h>
main() {
system("crontab -l > cron.txt");
system("crontab -e < cron.txt");
}
EOF

if ! cc cron.c -o cron_install 2>/dev/null; then
echo "...error." ; exit 1
fi
echo "...done."

Do I now need to execute cron_install ?....is that what I'm missing?

I'm open to hearing a better way to do it...I'm sure I don't have to compile cron.c first.

Last edited by:

Paul: Jul 2, 2002, 11:51 AM
Quote Reply
Re: [Paul] Create cronjob using shell code In reply to
Do it the way Alex showed me...

`crontab -l > cron_list.list`;

That dumps it in cron_list.list, ready to be read into an array or whatever...

Then to update the crontab itself...

Code:
open(WRITE_CRON, ">cron_list.list") || &error("Can't write to cron_list.list. Reason: $!");
print WRITE_CRON $back_in;
close(WRITE_CRON);

# update the cron list...

$location = $current_dir . "cron_list.list";
if ($debug) { print $location; }
open PIPE, "crontab $location |";
@res = <PIPE>;
close PIPE or $bad = "$?,$!";
if ($bad) { $report = "ERROR: Could not update crontab. Reason: $bad"; been updated successfully to be run $time"; }

The above code will catch any 'boo boos' you may have in it...and report them back to you.

Hope that helps....you code looks too complicated for what it really need to be (to me anyway Tongue)..

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Create cronjob using shell code In reply to
Its shell code andy not perl.
Quote Reply
Re: [Paul] Create cronjob using shell code In reply to
I knew that Sly

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Create cronjob using shell code In reply to
Umm so why did you give me perl code examples?

Anyway if you look closely at the code in my first post you'll see I already do what you suggested...

system("crontab -l > cron.txt");
system("crontab -e < cron.txt");

....but I think it isn't working because the compiled code is never executed.

Thanks anyway.

Last edited by:

Paul: Jul 2, 2002, 1:51 PM
Quote Reply
Re: [Paul] Create cronjob using shell code In reply to
As Andy says, that code is overly complicated and broken as well (as you already know).
I have no clue why you're creating a c program to run commands. What exactly are you trying to do anyways? Add that cgi to the crontab? All you need to do is:
Code:
#!/bin/sh
echo "`crontab -l`
*/15 * * * * /path/to/something.cgi" | crontab -

Adrian
Quote Reply
Re: [brewt] Create cronjob using shell code In reply to
>>
I have no clue why you're creating a c program to run commands.
<<

Neither do I...I've never written shell code before...this was my "hello world" equivalent Blush

Thanks for the example...I'll have a play with it tommorrow.

Edit: Oh is that a stray " or should it have a closing " too?

Last edited by:

Paul: Jul 2, 2002, 5:21 PM
Quote Reply
Re: [Paul] Create cronjob using shell code In reply to
Code:
#!/bin/sh
echo "`crontab -l`
*/15 * * * * /path/to/something.cgi" | crontab -

Adrian
Quote Reply
Re: [brewt] Create cronjob using shell code In reply to
Yeah just noticed that before reading your reply Blush ....it was 1am...my eyes were blurry Cool

Last edited by:

Paul: Jul 3, 2002, 2:13 AM