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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-30-2012, 01:14 AM
|
#1
|
|
Member
Registered: Aug 2012
Posts: 87
Rep: 
|
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.
|
|
|
|
10-30-2012, 01:37 AM
|
#2
|
|
Member
Registered: Jul 2009
Posts: 487
Rep:
|
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.
|
|
|
|
10-30-2012, 01:41 AM
|
#3
|
|
Member
Registered: Aug 2012
Posts: 87
Original Poster
Rep: 
|
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.
|
|
|
|
10-30-2012, 01:57 AM
|
#4
|
|
Member
Registered: Jul 2009
Posts: 487
Rep:
|
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 which would call the script above, which would call your other script.
If not you can do cron this way:
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.
|
|
|
|
10-30-2012, 02:02 AM
|
#5
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
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.
|
|
|
|
10-30-2012, 02:04 AM
|
#6
|
|
Member
Registered: Aug 2012
Posts: 87
Original Poster
Rep: 
|
Dear cbtshare,
How can i check its running?
|
|
|
|
10-30-2012, 02:11 AM
|
#7
|
|
Member
Registered: Aug 2012
Posts: 87
Original Poster
Rep: 
|
Dear jschiwal,
rid of the prompt means? sorry i m new to linux so i m not getting wat to do.
|
|
|
|
10-30-2012, 03:06 AM
|
#8
|
|
Member
Registered: Jul 2009
Posts: 487
Rep:
|
Quote:
Originally Posted by gajananh999
Dear cbtshare,
How can i check its running?
|
Verify cron is running by viewing log file, enter:
# tail -f /var/log/cron
|
|
|
|
10-30-2012, 03:27 AM
|
#9
|
|
Member
Registered: Aug 2012
Posts: 87
Original Poster
Rep: 
|
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?
|
|
|
|
10-30-2012, 04:06 AM
|
#10
|
|
Member
Registered: Aug 2012
Posts: 87
Original Poster
Rep: 
|
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.
|
|
|
|
10-30-2012, 04:52 AM
|
#11
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 15,021
|
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
|
|
|
|
10-30-2012, 05:12 AM
|
#12
|
|
Member
Registered: Aug 2012
Posts: 87
Original Poster
Rep: 
|
Dear chrism01,
Crontab is not running .sh file
|
|
|
|
10-30-2012, 05:16 AM
|
#13
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 15,021
|
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
|
|
|
|
10-30-2012, 05:47 AM
|
#14
|
|
Member
Registered: Aug 2012
Posts: 87
Original Poster
Rep: 
|
Quote:
Originally Posted by chrism01
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 05:28 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|