LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 11-16-2006, 12:51 PM   #1
a2vr6
Member
 
Registered: Aug 2006
Posts: 46

Rep: Reputation: 15
Cron job question


I was wondering if there is a way to setup a cron job that is to occur only once and then not run again? Is there something else I should be using?
 
Old 11-16-2006, 01:06 PM   #2
pljvaldez
Guru
 
Registered: Dec 2005
Location: Somewhere on the String
Distribution: Debian Squeeze (x86)
Posts: 6,092

Rep: Reputation: 269Reputation: 269Reputation: 269
I usually setup the job using a specific month and day, then just delete the cronjob the day after it runs.

I read somewhere that there's a couple commands you can use, at or batch to run a job just once. But I've never read the manpages to see how to use them.
 
Old 11-16-2006, 01:12 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
cron is for recurring tasks. at is what you need.
 
Old 11-16-2006, 01:53 PM   #4
amitsharma_26
Member
 
Registered: Sep 2005
Location: New delhi
Distribution: RHEL 3.0/4.0
Posts: 777

Rep: Reputation: 31
Quote:
Originally Posted by a2vr6
I was wondering if there is a way to setup a cron job that is to occur only once and then not run again?
Why do you wonder; if you are going to fill all 5 coloumns of the crontab scheduling; it so obvious that it will occur only once in a long long time.

e.g:
I plan to run once.sh at 04:10 a.m, 17th November only once.
Code:
10 4 17 11 fri /root/scripts/once.sh
And on the intersting part, if i can keep my box up till 17th november 2017 (which happen to be friday again), this script will run back again.
 
Old 11-16-2006, 01:54 PM   #5
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 2,032

Rep: Reputation: 309Reputation: 309Reputation: 309Reputation: 309
Just beware, at jobs get deleted from the queue if the system reboots before they are due. cron jobs will stick around when faced with a reboot, however if the system is down at the time they were scheduled to run they will not be executed. i.e., the system does NOT come back later and say, "Gee, I missed that one. I better run it now."
 
Old 11-17-2006, 11:18 AM   #6
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
Quote:
Originally Posted by amitsharma_26
Code:
10 4 17 11 fri /root/scripts/once.sh
And on the intersting part, if i can keep my box up till 17th november 2017 (which happen to be friday again), this script will run back again.
If I understand the crontab man page, that script would be run every Nov 17 and every Friday of every November. From the crontab(5) man page:
Quote:
Note: The day of a command's execution can be specified by two fields
-- day of month, and day of week. If both fields are restricted (ie,
aren't *), the command will be run when either field matches the cur-
rent time. For example,
``30 4 1,15 * 5'' would cause a command to be run at 4:30 am on the 1st
and 15th of each month, plus every Friday.
 
Old 11-17-2006, 12:46 PM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
OK, here's a pretty nasty hack... I created a script which can remove a specific line (well, comment out anyhow) from your crontab when the job runs...

It works by running your cron job with a wrapper called "crononce", like this:
Code:
10 4 17 11 fri /home/matthew/bin/crononce SOMEID /home/matthew/bin/myprog.sh
What crononce does is execute the command (and and arguments) specified after SOMEID, record the return status, and then edit the crontab, commenting out any lines with "crononce SOMEID " in it which are not already comments. You must modify SOMEID so that it is unique per use of crononce in your crontab, else the first one that runs will comment out all your instances with that ID. So your crontab file should look something like this if you have multiple jobs you want to execute once:
Code:
10 4 17 11 fri /home/matthew/bin/crononce ID1 /home/matthew/bin/myprog.sh
10 4 18 11 sat /home/matthew/bin/crononce ID2 /home/matthew/bin/myprog.sh
Well, here's the script. Be aware I HAVE NOT TESTED THIS (very much), so it might kill your whole system. Backup your crontabs first, and use at your own risk.

Code:
#!/bin/bash
# NAME:        crononce
# AUTHOR:      MNG 2006
# LICENSE:     GPLv2
# DESCRIPTION: execute this script once, and then remove the lines with ID $1
#              from the crontab (if it exists)

main () {
    if [ $# -lt 2 ]; then
        echo "ERROR: not enough arguments" 1>&2
        usage 1
    fi

    cronid="$1"
    shift

    $@
    errlev=$?

    # now to remove the line(s) from your crontab
    tmp=$(mktemp)
    crontab -l | sed -n "/^ *#/ {
                         p
                         d
                     }
                     /crononce[ \t][ \t]*$cronid[ \t]/ {
                         s/^/#/
                         p
                         d
                     }
                     p" > "$tmp"

    crontab "$tmp"
    rm -f "$tmp"

    exit "$errlev"
}

croned () {
    echo "called croned with args: $*"
    echo "cronid = $cronid"
}

usage () {
    cat <<EOD
Usage:
    ${0##*/} ID command [[arg] ...]

Executes command  with arguments, then edits the crontab file and comments any line which calls this script with ID as the first parameter.
EOD

    exit ${1:-0}
}

main "$@"
Enjoy
 
Old 11-17-2006, 12:48 PM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
I just had a thought... the editing of the crontab will probably fail if there is some manual editing going on, or there is another process modifying it. I suppose you could implement a wait loop around the crontab update. *sigh* this sort of thing is difficult to do reliably.
 
Old 11-17-2006, 02:33 PM   #9
amitsharma_26
Member
 
Registered: Sep 2005
Location: New delhi
Distribution: RHEL 3.0/4.0
Posts: 777

Rep: Reputation: 31
Quote:
Originally Posted by Berhanie
If I understand the crontab man page, that script would be run every Nov 17 and every Friday of every November. From the crontab(5) man page:
I stand corrected - yes that crontab scheduling will run on every november 17th & also every friday of every november. Thanks for pointing out.

regards,
Amit sharma.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
cron job every 2 seconds question ionic_slimm Linux - Certification 7 08-10-2006 07:22 AM
Simple Cron Job question FreezEy Slackware 3 07-11-2006 02:30 PM
Cron Job Question tarballed Linux - Newbie 4 06-20-2006 04:39 PM
Cron Job Question abhijeetudas Linux - Software 2 03-02-2006 12:27 AM
Quick Question about cron job andy18 Linux - General 2 08-11-2003 05:16 PM


All times are GMT -5. The time now is 01:04 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration