LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Run bash script as a daemon. (http://www.linuxquestions.org/questions/linux-newbie-8/run-bash-script-as-a-daemon-4175434701/)

gajananh999 10-30-2012 01:14 AM

Run bash script as a daemon.
 
Dear All,

I have written one script which will unzip the file and move to specified location

Code:

#!/bin/bash
FILES=/logs/isac/collation/data/server1/*.7z
for f in $FILES
do
 # echo "Processing $f file..."
 if ! 7za x -yo/logs/isac/collation/data/logs1/ "$f"
then
    echo "Extracting $f failed"
else
    echo "Extraction succesfull"
    mv "$f" /logs/isac/collation/data/archive/
fi
done

FILES=/logs/isac/collation/data/server2/*.7z
for f in $FILES
do
 # echo "Processing $f file..."
 if ! 7za x -yo/logs/isac/collation/data/logs2/ "$f"
then
    echo "Extracting $f failed"
else
    echo "Extraction succesfull"
    mv "$f" /logs/isac/collation/data/archive/
fi
done

I want to run this script every 5 min in the background may i know how to do this

I was using run services using daemontools my all c++ service i m running using daemontools but when i put the same in daemontools its not running i dont know what is the reason for this.

cbtshare 10-30-2012 01:37 AM

Cant you just setup a cron process? It will do as you ask : (your script is called unzip.sh)

Quote:

*/5 * * * * /location/unzip.sh &
or put the script in a loop with the sleep command

Quote:

#!/bin/bash
while true
do
/location/unzip.sh &
sleep 5
done

gajananh999 10-30-2012 01:41 AM

Dear cbtshare,

i didnt understand first option and second option will not work because when i do Ctrl+c this will stop the process

so i dont want to go with this.

cbtshare 10-30-2012 01:57 AM

umm, well I assume you are running this script on a unix based machine, so simple enter the command:
well you can run the process in the background by typing & at the end of the script .example
Quote:

./timer.sh &
which would call the script above, which would call your other script.

If not you can do cron this way:



Quote:

crontab -e
this will open the crontab for you in vi editor

press wto write and paste the command

Quote:

*/5 * * * * /location/unzip.sh &
then press : then type wq

jschiwal 10-30-2012 02:02 AM

You will want to rewrite the script to get rid of any prompts or echo statements. All printing (including command errors) should be to a log file or redirected to /dev/null. You could redirect stdout & stderr to a log file. Running a program in the background means it's not attached to a terminal. Use full path names for all commands. Cron runs in a scaled down environment.

gajananh999 10-30-2012 02:04 AM

Dear cbtshare,

How can i check its running?

gajananh999 10-30-2012 02:11 AM

Dear jschiwal,

rid of the prompt means? sorry i m new to linux so i m not getting wat to do.

cbtshare 10-30-2012 03:06 AM

Quote:

Originally Posted by gajananh999 (Post 4817910)
Dear cbtshare,

How can i check its running?

Verify cron is running by viewing log file, enter:
# tail -f /var/log/cron

gajananh999 10-30-2012 03:27 AM

Dear cbtshare,

Thanks a lot for your help.

I want to know one more thing suppose if reboot the machine then i have to do any changes or automatically it will run?

gajananh999 10-30-2012 04:06 AM

Dear cbtshare,

No its not able run the bash file. because there are so many .7z files in /logs/isac/collation/data/server1/ but is not doing unzip only.

chrism01 10-30-2012 04:52 AM

If you put it into cron, then it will restart automatically.
Also, in this case, the & is redundant.

If you run a cmd from the terminal manually and want it to keep running, even if you logout, you need nohup as well as '&' thus
Code:

nohup /path/to/prog >/path/to/prog.log 2>&1 &
http://rute.2038bug.com/index.html.gz
http://www.adminschoice.com/crontab-quick-reference

gajananh999 10-30-2012 05:12 AM

Dear chrism01,

Crontab is not running .sh file

chrism01 10-30-2012 05:16 AM

Quote:

I want to run this script every 5 min in the background may i know how to do this
As mentioned above by other respondents, using cron is the usual way to do this. You just need to re-direct the output to a log file

gajananh999 10-30-2012 05:47 AM

Quote:

Originally Posted by chrism01 (Post 4818021)
As mentioned above by other respondents, using cron is the usual way to do this. You just need to re-direct the output to a log file

I am not getting what exactly you are trying to say.


All times are GMT -5. The time now is 11:35 AM.