LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-19-2008, 07:00 PM   #1
Jesdisciple
LQ Newbie
 
Registered: Mar 2008
Posts: 11

Rep: Reputation: 0
Using 'AT' and 'CRONTAB' to Schedule Without User Input


This is a continuation of a conversation on another board where the other members lacked the expertise needed to help me. The other board focuses on PHP, but I need help with UNIX shell commands. The last post in that thread is at http://w3schools.invisionzone.com/in...dpost&p=100561 but I will quote the relevant part below.

Quote:
Originally Posted by Jesdisciple
I need to use a one-time AT to call a recurring CRONTAB. I think I understand AT (pass date and time in one command, and the scheduled command next?), but CRONTAB has me stumped. 'crontab -e' opens the current user's "cron table" in the vi text editor, but how do I submit a cron job by the command line? Here's what I have so far...
Code:
<scheduleCommand systems="Darwin SunOS Linux IRIX FreeBSD Mach" parameters="hour minute date"><![CDATA[
	at %hour%:%minute% %date%
	crontab -e
]]></scheduleCommand>
References:
http://www.hccfl.edu/pollock/Unix/AtDemo.htm
http://www.debianhelp.co.uk/schedulejobs.htm
http://www.ibm.com/developerworks/library/...scheduling.html
The XML 'scheduleCommand' element basically describes a programmatic function to any server script that reads it.

The 'systems' attribute specifies all kernel names that I know of which will support the function. (Please tell me of others if you know them!)

The 'parameters' attribute specifies the names of all function arguments; the server script will retrieve these and replace each %-enclosed name in the function body with its corresponding value, then execute the function body on the shell.

In case you need more info on my project, see http://code.google.com/p/error-logger/.

Thanks!

Last edited by Jesdisciple; 03-19-2008 at 11:29 PM. Reason: [QUOTE=Jesdisciple]
 
Old 03-19-2008, 07:04 PM   #2
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Is there a question in there somewhere?
 
Old 03-19-2008, 07:09 PM   #3
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
not sure exactly what you're trying to do... it sounds like you want to create a cron job with a script.... but cron jobs are recurring & it also sounds like you don't want them to be recurring. If that's the case, you want to use at. At can be done via the command line, executes once at the given time, then it's done.

if you want to edit a crontab via the command line, you can crontab -l to get the current crontab, edit it, then crontab <file> to update it.

So what exactly are you after?
 
Old 03-19-2008, 11:19 PM   #4
prad77
Member
 
Registered: Mar 2008
Posts: 101

Rep: Reputation: 15
you can use default cron file or user defined file for cron.
In either case it has to opened in editor for addition or modification.
To do it via code may be you could append the cron file with some redirection and execute crontab.

Fedora Development

Last edited by prad77; 04-17-2008 at 03:16 AM.
 
Old 03-19-2008, 11:23 PM   #5
Jesdisciple
LQ Newbie
 
Registered: Mar 2008
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by billymayday View Post
Is there a question in there somewhere?
The questions are in the quote. I quoted my own post from the other board.

Quote:
Originally Posted by BrianK View Post
not sure exactly what you're trying to do... it sounds like you want to create a cron job with a script.... but cron jobs are recurring & it also sounds like you don't want them to be recurring. If that's the case, you want to use at. At can be done via the command line, executes once at the given time, then it's done.

if you want to edit a crontab via the command line, you can crontab -l to get the current crontab, edit it, then crontab <file> to update it.

So what exactly are you after?
I need to use AT to schedule CRONTAB. The task to be scheduled is a database pruning. The initial command will be run when the database is initially installed and the pruning job will occur frequently (I think weekly, but I'm open to correction). But a record will need to be pruned only after a much longer period of time, I'm thinking a year would be about right. So pruning doesn't need to start until a year after installation, because of course a record cannot be older than its database. After that, it should occur weekly.

EDIT:
Quote:
Originally Posted by prad77 View Post
you can use default cron file or user defined file for cron.
In either case it has to opened in editor for addition or modification.
To do it via code may be you could append the cron file with some redirection and execute crontab.
I'm not sure what you mean by "append the cron file with some redirection"... Could you clarify please?

BTW, the database records errors, as the name 'error-logger' implies. BTW#2, I'm also having trouble with this in WinNT, in case anyone has experience with command-line scheduling there.

Last edited by Jesdisciple; 03-19-2008 at 11:36 PM.
 
Old 03-19-2008, 11:39 PM   #6
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Why not use AT to append a command to the user's crontab at the specified date. Something like

at now+1yr
>cat "30 5 * * 1 /path/command_to_run" > /var/spool/crontab/user
>[Ctrl D]

You may need to restart crontab for the change to take effect.

Not sure if I got the cronab entries correct or the location of crontab files (working from memory here)

Does the at queue survive reboots?

perhaps a neater solution would be to create a control file with the creation date of the database in it, and set the crontab entry to run each week from now, but to make the first thing it does to check that a year has passed from the date in the control file.

Last edited by billymayday; 03-19-2008 at 11:41 PM.
 
Old 03-20-2008, 01:16 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
It sounds like you want 2 things:

1. run once, immediately after the DB is created (ie make clean DB ?).
Answer: run it manually.

2. run eg weekly, but only after 1yr has passed from DB create/step 1 above
Answer: schedule it weekly in crontab anyway, but ensure it has a cfg file with the DB create/clean date in it and only
actually prune data if curr date is at least 1 yr > create date.
 
Old 03-20-2008, 01:58 PM   #8
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
Quote:
Originally Posted by Jesdisciple View Post
I need to use AT to schedule CRONTAB. The task to be scheduled is a database pruning. The initial command will be run when the database is initially installed and the pruning job will occur frequently (I think weekly, but I'm open to correction). But a record will need to be pruned only after a much longer period of time, I'm thinking a year would be about right. So pruning doesn't need to start until a year after installation, because of course a record cannot be older than its database. After that, it should occur weekly.
I hear what you're saying, but I'd like to think there is a better way of doing this... namely, why must you start after one year, then continue weekly? If you're continuing weekly, then that means new data is being added. If new data is being added, then your pruning would prune new data as well. If it doesn't prune new data, then there's no reason to wait a year before starting the cron job, as the pruning must be smart enough to only prune data that is older than X. Am I wrong in thinking this? can't you prune based on a timestamp in the DB?

I mean, what you're asking to do, while it may be possible, just doesn't sound like a good idea. What if you're no longer around after a year? What if you change your mind about the pruning? What if the db schema changes in that year period?

No offense, just throwing out some "what ifs".
 
Old 03-21-2008, 01:29 PM   #9
Jesdisciple
LQ Newbie
 
Registered: Mar 2008
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by billymayday View Post
Why not use AT to append a command to the user's crontab at the specified date. Something like

at now+1yr
>cat "30 5 * * 1 /path/command_to_run" > /var/spool/crontab/user
>[Ctrl D]
Does [Ctrl D] exit 'at'? And how can I simulate it?

Quote:
Originally Posted by billymayday View Post
You may need to restart crontab for the change to take effect.
From what I've read, this isn't necessary, but I would be more confident about that if someone else would confirm it.

Quote:
Originally Posted by billymayday View Post
Not sure if I got the cronab entries correct or the location of crontab files (working from memory here)
Where's the current user's home directory? http://en.wikipedia.org/wiki/Cron

Quote:
Originally Posted by billymayday View Post
Does the at queue survive reboots?
I think it would have to, but don't ask me.

Quote:
Originally Posted by billymayday View Post
perhaps a neater solution would be to create a control file with the creation date of the database in it, and set the crontab entry to run each week from now, but to make the first thing it does to check that a year has passed from the date in the control file.
That would work, although I don't see why it's neater.

Quote:
Originally Posted by chrism01 View Post
It sounds like you want 2 things:

1. run once, immediately after the DB is created (ie make clean DB ?).
Answer: run it manually.
Erm, I can't do that because I won't be there. See the below illustration. Anyway, I don't want to run the prune job that early, as there will be no records to prune. But that's when I need to schedule either the prune job or the other command that will schedule the prune job.

Quote:
Originally Posted by chrism01 View Post
2. run eg weekly, but only after 1yr has passed from DB create/step 1 above
Answer: schedule it weekly in crontab anyway, but ensure it has a cfg file with the DB create/clean date in it and only
actually prune data if curr date is at least 1 yr > create date.
Why is that better than using 'at'?

Quote:
Originally Posted by BrianK View Post
I hear what you're saying, but I'd like to think there is a better way of doing this... namely, why must you start after one year, then continue weekly? If you're continuing weekly, then that means new data is being added. If new data is being added, then your pruning would prune new data as well. If it doesn't prune new data, then there's no reason to wait a year before starting the cron job, as the pruning must be smart enough to only prune data that is older than X. Am I wrong in thinking this? can't you prune based on a timestamp in the DB?
Firstly, yes: My database does use timestamps. As for the rest, I think I need to illustrate what I'm thinking.

John, a system admin, installs my program on January 1, 2009, and JavaScript errors on his website immediately begin to be logged. Let's say he sets the prune job to run on Thursdays (in the installation page).

An error record isn't considered 'old' until more than one year has passed since its last occurrence. (Recurring errors increment the 'occurrences' field and update the timestamp.) So the program begins pruning on January 8, 2010 (a Thursday), and only records of those errors which haven't occurred since before January 8, 2009 will be pruned. No 'new' records would ever be pruned in this scenario.

(EDIT: I'm not sure if I will begin pruning exactly a year after installation or a year and a week... But you get the idea.)

Quote:
Originally Posted by BrianK View Post
I mean, what you're asking to do, while it may be possible, just doesn't sound like a good idea. What if you're no longer around after a year?
As illustrated above, I am not the system admin; I will never be "around," except for tech support and updates. (And as this is a free hobby project, those might not be spectacular.)

Quote:
Originally Posted by BrianK View Post
What if you change your mind about the pruning?
I do not expect the users (system admins) to modify the schema; in fact, I would recommend that they not do so as this could break my program.

Quote:
Originally Posted by BrianK View Post
What if the db schema changes in that year period?
If the user changes the program settings on the configuration page, I will modify the crontab as necessary. Or at least I plan to implement that feature.

Quote:
Originally Posted by BrianK View Post
No offense, just throwing out some "what ifs".
None taken.

Thanks for your time!

Last edited by Jesdisciple; 03-21-2008 at 01:35 PM.
 
Old 03-21-2008, 02:47 PM   #10
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Quote:
Where's the current user's home directory? http://en.wikipedia.org/wiki/Cron
Does the above answer anything? However the link to AT on the page may help (echoing to AT) http://en.wikipedia.org/wiki/At_%28Unix_command%29

Re post 6, user crontabs are in /var/spool/cron/, at least on RH systems, have look at their contents.

Last edited by billymayday; 03-21-2008 at 02:49 PM.
 
Old 03-21-2008, 02:58 PM   #11
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
Quote:
Originally Posted by Jesdisciple View Post
None taken.
well good. Then to fuel the constructive fire, why can't you just start a cron job immediately, but tell it not to work on anything that is less than 1 year old (or hasn't occurred for 1 year)? As I understand it, this is exactly what you're trying to do, but you're waiting 1 year before you get started. What I'm proposing will start working immediately, but actually not do anything for one year (because everything will be newer than one year).

Of course, I may be missing a part of the picture, but removing a level of indirection will help simplify things for both yourself and the end user. ... and it's easier to find (and stop) a cron job than an at job, at least for basic users.

just wanted to add that. Good luck however you solve it!
 
Old 03-21-2008, 03:40 PM   #12
Jesdisciple
LQ Newbie
 
Registered: Mar 2008
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by billymayday View Post
Does the above answer anything? However the link to AT on the page may help (echoing to AT) http://en.wikipedia.org/wiki/At_%28Unix_command%29
"The above"? I don't understand where/what you're referring to. EDIT: And I'm not sure what I need to see on that page.

Quote:
Originally Posted by billymayday View Post
Re post 6, user crontabs are in /var/spool/cron/, at least on RH systems, have look at their contents.
So is that a dynamic directory, i.e. with different contents when a different user is logged on?

Quote:
Originally Posted by BrianK View Post
well good. Then to fuel the constructive fire, why can't you just start a cron job immediately, but tell it not to work on anything that is less than 1 year old (or hasn't occurred for 1 year)? As I understand it, this is exactly what you're trying to do, but you're waiting 1 year before you get started. What I'm proposing will start working immediately, but actually not do anything for one year (because everything will be newer than one year).

Of course, I may be missing a part of the picture, but removing a level of indirection will help simplify things for both yourself and the end user. ... and it's easier to find (and stop) a cron job than an at job, at least for basic users.

just wanted to add that. Good luck however you solve it!
Isn't it inefficient to execute an SQL query that will certainly do nothing? For that matter, isn't it inefficient to check if a year has passed each week for a year? This is the idea behind using 'at'; it's a trade-off of simplicity for efficiency.

Yet avoiding 'at' for the sake of not needing to edit its queue does seem like it might be worth the inefficiency...

Last edited by Jesdisciple; 03-21-2008 at 03:50 PM.
 
Old 03-21-2008, 04:28 PM   #13
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
Quote:
Originally Posted by Jesdisciple View Post
Isn't it inefficient to execute an SQL query that will certainly do nothing? For that matter, isn't it inefficient to check if a year has passed each week for a year? This is the idea behind using 'at'; it's a trade-off of simplicity for efficiency.

Yet avoiding 'at' for the sake of not needing to edit its queue does seem like it might be worth the inefficiency...
RE Inefficient: well, being that a computer's clock does on the order of a few billion ticks per second, using an extra few billion ticks once per week is all but meaningless. Even if the operation takes 10 full seconds to complete, you're still only using ~9 minutes of CPU time per year - which amounts to nothing... much less time than many other deamons take. Same goes for the disk access required. Of course, this is a point for debate & is definitely a design issue; so I suppose there's no right or wrong. However, if it were me, the less support I have to do and the less jumping-through-hoops I have to do, the better. If it were me, I would absolutely take the few minutes or so of bottleneck per year (hell even an hour or a full day of CPU time spread over a year would be fine) to avoid scheduling something a full year from now... especially when that something is another scheduler.

I would say make the timestamp column an index & it should be pretty blazingly fast.

Have fun!
 
Old 03-21-2008, 04:31 PM   #14
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Quote:
echo "cc -o foo foo.c" | at 1145
That bit

No it's not dynamic as such - each user has a crontab that is used whether or not they are logged on.

What's so bad about waisting a couple of hundred CPU ctcles a wee - and what overhead does an active AT queue add? I still stick to my view that you should check a datestamp.
 
Old 03-21-2008, 11:21 PM   #15
Jesdisciple
LQ Newbie
 
Registered: Mar 2008
Posts: 11

Original Poster
Rep: Reputation: 0
OK, I agree with you now, especially since Apple uses 'launchd'... I don't need a third compatibility branch.

Quote:
Originally Posted by billymayday View Post
That bit
What's that have to do with where the user's home directory is? BTW, this is what I was talking about at http://en.wikipedia.org/wiki/Cron:
Quote:
1. On start-up, look for a file named .crontab in the home directories of all account holders.
Now I'm trying to understand this:
Quote:
cat "30 5 * * 1 /path/command_to_run" > /var/spool/crontab/user
I get the redirection, and that this is supposed to append the quoted characters to the crontab. But I don't find anything to say that 'cat' can write a string to a file. Wouldn't it rather be this?
Quote:
echo "30 5 * * 1 /path/command_to_run" >> /var/spool/crontab/user
Thanks!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 06:40 PM
linux user login schedule sem007 Linux - Enterprise 1 12-23-2006 05:07 AM
schedule a crontab rupesh_pulikool Solaris / OpenSolaris 7 09-04-2004 09:32 PM
crontab schedule help onlykims Linux - Newbie 2 08-28-2004 12:09 PM
Schedule TOP command in Crontab. imsajjadali Red Hat 2 05-20-2004 04:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration