ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
sometimes I run a script or program which takes a while to run (like compiling something). Half through, I realise that I would like to run a command afterwards (say, reboot or beep). Had I realised earlier, I could have done
cmd1 ; reboot
But if it's already running, is there anyway to tag on subsequent commands? I mean, it is possible to pause it with a CTRL-Z in the bash shell.
If not, I may try writing an app that I can run in the CTRL-Z space, like checkApp cmd1 -[cmd2 cmd3] & ( ; fg 1), which greps ps -ef to see if cmd1 is still running, and if not run cmd2, cmd3 etc. Perhaps one exists though?
Does something like this already exist, or is there a way of tagging on subsequent commands?
Thank you for your time.
---EDIT
by Jojo http://www.votelinux.com/tips/144-Re...ram-is-running
Here is a little bash script which will monitor the status of any process. Just call it with the processname (or a part of it) and it will loop endlessly unless the given process has quit.
This is very useful to monitor the status of daemons on a server
A simple way to do this, is:
1) Stop the command execution with Ctrl-Z
2) Put it in background with command bg
3) Type the following command and press enter
Code:
wait $! && echo done
The wait command is a shell builtin that waits for a process to accomplish its task and return the exit command of that process (see man bash for details). The $! means "last background process". So, after you have put the command in background you wait for its completion and when the command has successfully completed the command after && is executed. More difficult to explain than to do!
Also, I can run a command, then halfway into it just type the next command and push Return and it will run the second command when the other one is finished.
It is a really different thing: $! takes the "last background job" !$ takes the "last word of the previous command"
As you can see they are totally unrelated.
Oh, this is juicy stuff! I knew about using $! but not the other one. Perhaps one of you can help with a related question. How do you 'fish' for variables deaclared before the last command, or before the current process?
What I mean is, I start a process like this:
VAR=SomeValue program-name
program-name is a BASH script of course. How can find out what was given before
program-name on the command-line?
prog-name is the program that gets its' progress tracked with a bar. It must be backgrounded like: 'prog-name &'. Then when calling the function rotdash2, the &! makes it attach the last background process to the rotdash2 program. BTW, that code is mostly credited to Klaus Knopper -I just had to do some minor changes in the escape characters or something for use with BASH. The original code was for ash-knoppix.
$! is the process id fed to p=$1
while [ -d /proc/$p ] tracks the exisitng of the process
all the echoes can be replaced by a simple sleep if you just want poll
to see if the process is running. Be sure to sleep or do something in the loop or you have a run-away process.
Those funny characters are BS (back-spaces). Whats the escape sequence for that? Wait, I've been playing with that, look at the edited code above.
echo -e \E[B backups one character. Using \E[b backs up and repeats the prvious char?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.