hi all,

i want to create a loop with this bash script and make it run continuesly

#!/bin/bash
mkdir /graph
touch /graph/.duc.db
chmod -R 777 /graph
time /usr/local/bin/duc index -d /graph/.duc.db -p /shares/
sed -i -e ‘s/pie/graph/g’ /var/www/cgi-bin/duc.cgi
rm -rf /pie

mkdir /pie
touch /pie/.duc.db
chmod -R 777 /pie
time /usr/local/bin/duc index -d /pie/.duc.db -p /shares/
sed -i -e ‘s/graph/pie/g’ /var/www/cgi-bin/duc.cgi
rm -rf /graph

basically this script re-indexes the database but instead of creating the database in the same file it creates it in a new folder and file and just replaces the folder in var www cgibin

does anyone know the correct syntax for the loop

many thanks,

rob

5 Spice ups
while true
do
   your commands ....
done
3 Spice ups

wow just while true, thought i would need to create a variable?

Why? The statement’s condition is always true, with no condition. It would be the same as setting a variable, then assigning the condition to the while statement.

1 Spice up

so i would just run the script once on the host machine and then it will carry on running it indefinetly withouht having to put it in cron?

1 Spice up

Wouldn’t this be a great opportunity to use cron? Then you would have the system running the job regardless of what might happen. You could log the status as well.

2 Spice ups

yes i could but if the script is in a loop no need for cron really, is there?

boom, got it

@reboot /scripts/duc.sh

#!/bin/bash

while true

do

mkdir /graph
touch /graph/.duc.db
chmod -R 777 /graph
time /usr/local/bin/duc index -d /graph/.duc.db -p /shares/
sed -i -e ‘s/pie/graph/g’ /var/www/cgi-bin/duc.cgi
rm -rf /pie

mkdir /pie
touch /pie/.duc.db
chmod -R 777 /pie
time /usr/local/bin/duc index -d /pie/.duc.db -p /shares/
sed -i -e ‘s/graph/pie/g’ /var/www/cgi-bin/duc.cgi
rm -rf /graph

done

True, but cron would log it. Plus, it makes for cleaner code. Well, eliminating two lines of code, but still, it makes it easier to manage. Especially, if you need to change the timing on it later.

2 Spice ups

My instincts have me siding with digital0ak–put it in cron.

But if not in cron, you’ll want to set it up to run when the system boots.

You might want a job in cron to check on the script from time to time.

If you do put in cron, the job should test to see if the script is already running, and exit if it is. This would let you use your “while true” loop in a script run by cron.

i mean add it to crontab -

@reboot /scripts/duc.sh

Yup

how do i make a cronjob run every 12 hours but it starts from 5am, so the next one will be 5pm?

* * * * * *
| | | | | | 
| | | | | +-- Year              (range: 1900-3000)
| | | | +---- Day of the Week   (range: 1-7, 1 standing for Monday)
| | | +------ Month of the Year (range: 1-12)
| | +-------- Day of the Month  (range: 1-31)
| +---------- Hour              (range: 0-23)
+------------ Minute            (range: 0-59)

So your cron job would be:

0 5,17 * * * /your/script/here

2 Spice ups

Don’t forget to use full path to script or use PATH and make script executable.

Also, it’s better to use local user instead of root. Here is a most popular mistakes with cron .

Use cron, otherwise, this script will be forgotten if the server is ever restarted. Automation is much better than my memory!

1 Spice up

Take the script to the next level and have it look for an instance of itself, that way you will NOT have more than one instance running. Have the cron job kick off and the script should check for a running copy of itself 1st. if it finds it, exit gracefully.

I have one service that uses java, and every 3-4 months, it stops. Since it always happens when I am in deep slumber - I have a bash script run a cron job every 10 minutes and start the service if it is not running (and email me when it happens). It is far better than the warehouse calling at 2am :wink: