LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Make a bash script into a daemon (http://www.linuxquestions.org/questions/linux-newbie-8/make-a-bash-script-into-a-daemon-896051/)

DuskFall 08-07-2011 12:48 PM

Make a bash script into a daemon
 
So i've written a script that will move some files from one directory to another, i dont know if this is the right place to ask but, this is on my ipod touch and i'm wondering how to make it run periodicaly
Thanks in advance

paulmarc 08-07-2011 01:53 PM

Crontab is used to run scripts periodically
 
You can have it run periodically by adding your script in a crontab.
To do that, execute the following:
Code:

[username@hostname ~]$ crontab -e
That should open your default shell editor, where you can specify crontabs, which are commands run periodically.
To make your script run periodically, add the following line:
Code:

MM HH dd mm ww /the/full/path/to/your/script
Where:
  • MM: Minutes (0-59)
  • HH: Hours (0-23)
  • dd: Day of month (1-31)
  • mm: Month (1-12)
  • ww: Day of the week (0-7), 0=7=Sunday
  • Use '*' to denote 'any' or 'every'
You can check if your crontab was stored by executing:
Code:

[username@hostname ~]$ crontab -l
For example, to have your script run each day, at 16:00, add the following line:
Code:

00 16 * * * /the/full/path/to/your/script
Note that it's important to include the full path of your script, to make sure it runs.
As a rule of thumb, to test if your script will run properly, go to your home directory and run your script by calling it using the full path.
If it runs correctly, you're good. You can always put a crontab that run in few minutes, and check if it does run properly.

Let me know if you need assistance in editing your crontab.

NOTE: Do you intend to run the script from your iPod, periodically? If yes, how do you access it? Via SSH or local terminal?

DuskFall 08-07-2011 02:58 PM

I've written it and tested it in local terminal, and it works fine, but i dont know how to A) see if its running or functioning, and B) sorry but i dont think my ipod has crontab, i looked for it with ps -A but there was no reference to it. And yes i'm running it via local terminal on the device. Thank you for helping...

paulmarc 08-07-2011 04:01 PM

Need cron on iPod, can use launchd
 
To see if your script is running, try outputting things to a file during its execution, and open that file to see if it's indeed running.
Now, "B" is the issue, and we can fix it in 2 ways:
  1. Install cron on iPod
  2. Use launchd
I don't know if there's yet a cron port to the iOS :scratch:, so "1" might be a problem.
However, we can do "2", since launchd comes on iOS.
To use launchd, you need to create an XML plist file in [/Library/LaunchDaemons], do a couple of steps and that's it :)...
Create the file [/Library/LaunchDaemons/com.scripts.myscript.plist] and have its content as follows (in bold what you need to change):
Code:

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE plist PUBLIC “-//Apple//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
<plist version=”1.0″>
<dict>
    <key>Label</key>
    <string>scripts.myscript</string>
    <key>OnDemand</key>
    <true/>
    <key>Program</key>
    <string>/the/full/path/to/your/script.sh</string>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>16</integer>
        <key>Minute</key>
        <integer>0</integer>

    </dict>
</dict>
</plist>

The key tags for the cron are follows:
  • Minute
  • Hour
  • Day
  • Weekday
  • Month
They should be followed by an <integer> tag, to specify their value.
If you don't specify a tag, it will be as if you used '*' in cron.
The plist file name can be whatever you want, mind you, so use what you think is logical.
Now, you need to let launchd know about your plist entry, so execute:
Code:

iPod:~ mobile$ launchctl load /Library/LaunchDaemons/com.scripts.myscript.plist
And it's done (hopefully)!
You can always check the launchctl man page.

DuskFall 08-07-2011 04:28 PM

YES, thats exactly what I was after, thank you so much :) I'd pieced together parts of a launchdaemon but didnt know you have to let launchctl know its there... Thank you so much :)

DuskFall 08-07-2011 04:34 PM

Thinking about it, how would I make it run every 5 minutes?

paulmarc 08-08-2011 04:46 AM

Can have multiple time entries
 
You can have multiple <dict><key> time entries, like:
Code:

    <dict>
        <key>Minute</key>
        <integer>0</integer>
    </dict>
    <dict>
        <key>Minute</key>
        <integer>30</integer>
    </dict>

That would run each 30 minutes...
Now, I don't know how to make it in another way, but if you want to have it each 5 minutes using this method, you need 12 entries :eek:...


All times are GMT -5. The time now is 07:56 AM.