LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-15-2007, 12:34 PM   #1
riotxix
LQ Newbie
 
Registered: Jul 2005
Location: England
Distribution: slackware (2.6.10)
Posts: 19

Rep: Reputation: 0
Adding commands when it's too late


Hi,

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

#!/bin/bash

while ps ax | grep -v -e grep -e [scriptFileName] | grep -q $1 ; do
echo "Programm $1 running"
sleep 1
done
------

Last edited by riotxix; 02-15-2007 at 03:31 PM.
 
Old 02-15-2007, 01:09 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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!
 
Old 02-15-2007, 01:11 PM   #3
Samoth
Member
 
Registered: Apr 2005
Distribution: Exherbo
Posts: 474
Blog Entries: 1

Rep: Reputation: 32
Isn't that "!$"?

because I know that if I do
Code:
echo x >> foo && vi !$
I will edit foo.

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.

Last edited by Samoth; 02-15-2007 at 01:12 PM.
 
Old 02-15-2007, 01:14 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 02-15-2007, 01:43 PM   #5
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
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?


Here's the way I use $!

Code:
rotdash2()
{
  p=$1
  while [ -d /proc/$p ]
  do
    echo -en '\E[32;40m/\E[B' ; sleep .05
    echo -en '\E[32;40m-\E[B' ; sleep .05
    echo -en '\E[32;40m\\E[B' ; sleep .05
    echo -en '\E[32;40m|\E[B' ; sleep .05
    echo -en '\E[32;42mS' ; tput sgr0 ; sleep .2
  done
}

prog-name &
rotdash2 $!
That makes a nice little CLI progress-bar (green) with a rotating star-looking cursor

Last edited by gnashley; 02-17-2007 at 06:25 AM.
 
Old 02-15-2007, 07:36 PM   #6
Samoth
Member
 
Registered: Apr 2005
Distribution: Exherbo
Posts: 474
Blog Entries: 1

Rep: Reputation: 32
Ah yes, my mistake.

That looks interesting, gnashley, but how exactly do you call it? Does that program take the place of $!?
 
Old 02-16-2007, 12:54 AM   #7
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
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.
 
Old 02-16-2007, 09:35 AM   #8
Four
Member
 
Registered: Aug 2005
Posts: 298

Rep: Reputation: 30
How does it know how far the program is into? Also I see a strange symbols at the '\E[32;40m/' last character of each of those statments.

Thanks
 
Old 02-17-2007, 06:27 AM   #9
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
$! 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?
 
Old 02-19-2007, 07:11 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
I don't get it !?!?!
if you type in a command after something has started running it will run afterwards anyway.

the keyboard doesn't turn off when a command runs.
 
  


Reply

Tags
command



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
Adding Commands canuck_barlow Linux - Newbie 3 09-29-2006 02:25 AM
Adding Boot Commands pjpeter Linux - Newbie 6 01-25-2005 08:10 PM
adding my own commands to rc files SizofreNICK Slackware 8 11-07-2004 02:30 AM
adding commands to /bin (root user) xone Linux - Software 4 03-23-2004 12:26 PM
adding commands? megaman Linux - Newbie 2 12-20-2003 03:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:04 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration