LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 10-30-2012, 01:14 AM   #1
gajananh999
Member
 
Registered: Aug 2012
Posts: 94

Rep: Reputation: Disabled
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.
 
Old 10-30-2012, 01:37 AM   #2
cbtshare
Member
 
Registered: Jul 2009
Posts: 569

Rep: Reputation: 42
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

Last edited by cbtshare; 10-30-2012 at 01:39 AM.
 
Old 10-30-2012, 01:41 AM   #3
gajananh999
Member
 
Registered: Aug 2012
Posts: 94

Original Poster
Rep: Reputation: Disabled
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.
 
Old 10-30-2012, 01:57 AM   #4
cbtshare
Member
 
Registered: Jul 2009
Posts: 569

Rep: Reputation: 42
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

Last edited by cbtshare; 10-30-2012 at 02:00 AM.
 
Old 10-30-2012, 02:02 AM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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.

Last edited by jschiwal; 10-30-2012 at 02:07 AM.
 
Old 10-30-2012, 02:04 AM   #6
gajananh999
Member
 
Registered: Aug 2012
Posts: 94

Original Poster
Rep: Reputation: Disabled
Dear cbtshare,

How can i check its running?
 
Old 10-30-2012, 02:11 AM   #7
gajananh999
Member
 
Registered: Aug 2012
Posts: 94

Original Poster
Rep: Reputation: Disabled
Dear jschiwal,

rid of the prompt means? sorry i m new to linux so i m not getting wat to do.
 
Old 10-30-2012, 03:06 AM   #8
cbtshare
Member
 
Registered: Jul 2009
Posts: 569

Rep: Reputation: 42
Quote:
Originally Posted by gajananh999 View Post
Dear cbtshare,

How can i check its running?
Verify cron is running by viewing log file, enter:
# tail -f /var/log/cron
 
Old 10-30-2012, 03:27 AM   #9
gajananh999
Member
 
Registered: Aug 2012
Posts: 94

Original Poster
Rep: Reputation: Disabled
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?
 
Old 10-30-2012, 04:06 AM   #10
gajananh999
Member
 
Registered: Aug 2012
Posts: 94

Original Poster
Rep: Reputation: Disabled
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.
 
Old 10-30-2012, 04:52 AM   #11
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
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
 
Old 10-30-2012, 05:12 AM   #12
gajananh999
Member
 
Registered: Aug 2012
Posts: 94

Original Poster
Rep: Reputation: Disabled
Dear chrism01,

Crontab is not running .sh file
 
Old 10-30-2012, 05:16 AM   #13
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
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
 
Old 10-30-2012, 05:47 AM   #14
gajananh999
Member
 
Registered: Aug 2012
Posts: 94

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by chrism01 View Post
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.
 
  


Reply

Tags
daemon, script


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
[SOLVED] Make a bash script into a daemon DuskFall Linux - Newbie 6 08-08-2011 03:46 AM
bash script running as daemon Satriani Programming 12 03-29-2011 05:23 PM
Run my bash script as a daemon. jaimese Linux - Newbie 12 02-10-2011 03:28 PM
Bash script to verify the daemon is working if not, start the daemon kishoreru Linux - Newbie 7 09-23-2009 04:29 AM
Run init script as daemon isuck@linux Linux - Software 5 02-12-2007 01:29 AM


All times are GMT -5. The time now is 02:48 AM.

Main Menu
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