jdlev has asked for the wisdom of the Perl Monks concerning the following question:

Well! My program is finally done! HORAY! It's a program that monitors a database for new records and emails them as new ones come up...my first real program...that actually works!

I've set it up to sleep for 5 minutes at a time after it runs through a loop. I would like to have the program run as soon as the computer comes on, and continue running in the background. Right now I can only call it from the cmd prompt. How do I automate it to start when my computer starts and get it to run unnoticed in the background?

Thanks!

I love it when a program comes together - jdhannibal
  • Comment on File Automation & Running In The Background

Replies are listed 'Best First'.
Re: File Automation & Running In The Background
by tilly (Archbishop) on Jan 22, 2009 at 03:07 UTC
    It would help a lot to know what operating system you are on. It may also require administrative privileges. Plus you need to think about what you want to do if it is accidentally killed.

    One trick that I've used to solve these problems is to have a cron job that tries to start my program every 5 minutes if it isn't already running. That is great if you are on Unix, and isn't hard to set up. Just use the crontab command with a line like this:

    */5 * * * * /path/to/your/command and args > /dev/null

    On Windows you can use the Windows scheduler for this instead.

Re: File Automation & Running In The Background
by graff (Chancellor) on Jan 22, 2009 at 05:18 UTC
    As tilly points out, what needs to be done will depend on which OS you have. Apart from that, once you have the appropriate cron/scheduler solution that would make sure your program is always doing its job, it would probably be better to restructure your program as a "one-shot" thing instead of an "infinite-loop-sleep-for-5-minutes" thing.

    That is, just set up a schedule that will run this monitoring script once every 5 minutes, and emails will be sent at that interval when appropriate. (If it needs to preserve state information from one interval to the next, just store that in a disk file.)

Re: File Automation & Running In The Background
by lakshmananindia (Chaplain) on Jan 22, 2009 at 08:00 UTC

    If you use linux then you can achieve this by using the init.d. Also refer the man page of start-stop-daemon to make your program start stop restart etc..