ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
i would like to write a shell-script which should do the following:
For an example the scripts name ist TEST...you run
....#TEST & (to put it to the background)
The script is running in a loop and waiting for a special command, f.e "mycommand"..
Due to this fact the script is running in the background I can further work on my console prompt and when I write the "mycommand" the script will identify that I wrote "mycommand" and will do what it have to....
So I need a way to catch my console-input by a shell-script....Anybody an idea??
You don't need a shellscript for that. Just use command 'fg' to get back to the program you've put in the background.
And catching console input from within the shell is a costly operation. I've written something like that in perl ( using the select system call ), but system-load was going very high, due to a lot of interrupts and the overhead a scripting language has...
I don't have the same experience as fvgestel in capturing user input, but I come to the same conclusion: don't.
This is just far too complicated to mess with. I'd suggest an alternative. This might be useful:
1. Start the program by running it in the background
2. Have the script periodically check for the existence of a specific file (this is your flag)
3. Create an alias for your "mycommand" so that it will touch/create the file the script is looking for (and anything else it needs to do)
4. When the script sees the file is present, perform whatever action is necessary
5. Have the script delete the flag-file when it's done
Using something like that avoids the complexity of sharing user input between your script and the shell, and it accomplishes the same thing (from the user's perspective). The difference is, there's some added latency. If your script checks for the file every X seconds, then there's the chance the script won't perform its task for X-1 seconds after "mycommand" is issued. X can be reduced to minimize this delay, but the smaller X gets, the more time and resources spent checking for the file.
Another alternative would have the script capture a signal. I don't have any experience with signal handling in scripts so I can't help there. However, a signal would cause the script to immediately begin its task after "mycommand" is issued. This method would still require aliasing "mycommand." The alias would probably need to contain a kill command to send the specific signal.
# Aliases for root
#if [ -e "/root/.bashrc" ]; then
# cat alias.conf >> /root/.bashrc
# echo "An error occured..."
while [ "$weiter" != "quit" ]; do
if [ -e "/tmp/gsx.flag1" ]; then #Info: When alias "d" is pressed, in /tmp the file "gsx.flag1" will be created!!!
echo "[daemon] [start/stop/restart]:"
read a b
elif [ -e "/tmp/gsx.flag2" ]; then #When alias "q" is pressed, in /tmp the file "gsx.flag2" will be created!!!
echo "Quitting gsx-daemon...."
I start the script in the background:
lowrider:/home/gsx/scripts# bash test &
# Then the apache2-script show me the usage-text and don´t start the daemon!!
# Additionally the job will be stopped
+ Stopped bash test
--> What´s the error in the script? Why the daemon could not be started...when i type "/etc/init.d/apache2 start" (at normal cmd-prompt) it works fine..
--> When the job is stopped (like above) how to get it running again? First kill and restart or is there aa other way?
--> When i press "q" to exit the script (still in background) the script exit´s but i don´t have an command prompt. I have to type in a further command and the the job will be closed...is there any possibility to exit the script immediately?
this issue we talking about is only a small part...
I am writing a script which do a lot of things....f.e. Configuring the Xserver, starting daemons, change your grub-settings and so on.....
I want to give the user two options to use the script:
1. Like you said only as shell-script (work with parameter like $1,$2,...)
2. To load the script at boot into background and to use just a lot of simple commands to do the things for you...it�s more comfortable...