Monitor console output - linux

I have a program, lets call if 'foo'
Foo works fine for a random amount of time during which it announces its progress on the console.
But after sometimes it stops giving any output. At this point I have to manually close the program (ctrl + c) and start it again.
I would like to know if there is a way to monitor console output of a program and in case there is no output for a certain duration of time take some action.
Platform is linux.

I've found this on Internet about a command called watch.
Name
watch - execute a program periodically, showing output fullscreen
Synopsis
watch [-dhvt] [-n ] [--differences[=cumulative]] [--help] [--interval=] [--no-title] [--version]
Description
watch runs command repeatedly, displaying its output (the first screenfull). This allows you to watch the program output change over time. By default, the program is run every 2 seconds; use -n or --interval to specify a different interval.
The -d or --differences flag will highlight the differences between successive updates. The --cumulative option makes highlighting "sticky", presenting a running display of all positions that have ever changed. [...]
watch will run until interrupted.

Related

Printer Queue in MacOS

I have a LiveCode app standalone that needs to know if there is a job waiting in the MacOS print queue before printing. If app user 1 prints the 2 page report and just one page prints (out of paper) then user 2 comes along and prints the report, the first page out is user 1's report and this is causing mixups. I would like to check the MacOS print queue and prevent printing if there is a job already waiting.
It's not something I've ever needed to do, but I suspect that this capability is not included in LiveCode natively. Instead your best bet will probably be to use LiveCode's shell() function to run a unix terminal command. For instance, lpstat is a command line utility that allows you to query various things about printers connected to your Mac. The following command, run in the MacOS terminal, shows which printers are available and their current status.
lpstat -p
In LiveCode you use the shell() function to call this command line utility, like so:
put shell("lpstat -p") into tPrinterStatus
To find out more about lpstat, open the Terminal and look up the man page:
man lpstat
Lots of options for that utility will appear. There should be one that gives you the information you need.

Linux: How to cat stdout of a process until the end, but stop and return a non-zero code if certain string appears?

We have a tool that runs tests, but does return an error code if they fail.
The tool runs the tests after starting logging in through SSH to a custom console (not bash) and issuing a command. All tests run at once within that invocation
The logging of the tests goes to a file.
The output of the tool is roughly:
test1 [ok]
test2 Some message based on the failure
...
To stop the build, we need to look for certain strings in the output.
The output appears as the tests run.
I could capture the whole output into a file and fail at the end. But it would save quite some time to fail once the first test fails.
Therefore, I would like something like tee, but it would also kill the execution if it finds that failure string. Or, at least, it should print the output as it comes, and return non-zero if a string is found.
Is this doable with the standard Linux toolkit?
The only solution I can think of is:
Start you build process and cat its output to an output file.
Start another script that monitors this file: a loop which iterates every X seconds in search for your, lets say, forbidden words in the file. As soon as they appear, kill the build process (you may need a way to identify your build process, such as a pid file or something like this) and clear the file.
You can even put this 2 processes in a single shellscript and make them both start and stop when needed.

Is there a way to replicate pressing the enter key in one terminal from another?

I have a script that runs a program that generates an email and then waits for the user to input information from that email. Because of this, the information is not available to pipe to the program on execution. It has to be done after it's running already.
I've tried a number of the different method suggestions, redirecting to the terminal (echo or cat > /dev/pts/#), mkfifo, expect, etc. Most of these methods are successful in actually sending the information to the target terminal - I actually see the information show up there. But there doesn't seem to be a way to send the equivalent of someone pressing the enter key.
Newline (using echo -en) or \n inside of the information send actually makes the cursor move in the target terminal - but it's not the same as actually pressing enter. It just scrolls the screen down one line. If I select the terminal window and press enter, it then accepts the input.
How can I do this from a different terminal (or can I?)
Or better yet, is there a way from the same script that's launched the program now waiting for input to send the program input?
Most of what I've seen has gone the route of using another terminal to try to send the information to the program.
Flow:
Simple similar example is if you were running a shell script that during its execution, runs a program that generates a PIN sent to your email, and then waits for the PIN to be entered before continuing.
In terminal 1, a bash script is run - one of the steps this script does is launch a program that generates an email and then sits at a prompt, waiting for the user to provide input (data from the email sent).
In terminal 2 (or if you can do this through terminal 1 somehow), take the value from the email and input it to the program in terminal 1, including pressing the enter key, so the program sees this value as actually entered and the program can complete its execution.
script in terminal 1:
...
if [ -f $progamToRun ]; then
$programToRun # This program sends the email and prompts the user for information from the email. It waits here...
# once information from email is entered, continue executing commands here
fi
script in terminal 2:
...
# monitor email
# retrieve information from email
# send information to program in terminal 1 that is waiting for this input
# exit script
The steps for the script in terminal 2 are working - and all of the methods I've tried to send the information to the program in terminal 1 haven't worked - that is to say, even if the information appears in the terminal through STDIN, etc. the application running doesn't recognize it as input so it's still sitting in the wait state.
I attempted to use netcat as suggested, but that's failing differently.
In terminal 1 I ran:
cat /tmp/nctest | /tmp/docl.sh -i 2>&1 | nc -l 127.0.0.1 8080 > /tmp/nctest
docl.sh contains one line, the application I am running to generate the email and wait.
In terminal 2 I ran:
nc 127.0.0.1 8080 <information from email>
When I run this in terminal 2, I immediately get the error from the program run in the script in terminal 1 that it failed. I tried this:
nc 127.0.0.1 8080
by itself to see if there was an issue passing the information on the same line I run nc, but get the same result. It's like the first terminal's nc command actually runs the application and then somehow bypasses or processes the prompt, so it's gone before I can enter the information from the email.
I also tried the command in terminal 1 without the -i and without the 2>&1, but there was no difference in the result.

How to QLOGIN to a node that shared a specific job-id

I have an existing qlogin job like this:
job-ID prior name user state submit/start at queue
-------------------------------------------------------------------------
3530770 0.50500 QLOGIN jlsmith r 10/15/2012 14:02:07 mri.q#compute-0-29.local
The above job was submitted using standard qlogin command in linux:
$ qlogin
What I want to do is to perform another qlogin so that the process
are running in the same node with the above Job-ID 3530770.
The idea is that if it's done correctly in top command I can see the same running
process submitted to the above job-ID.
Is there a way to do it?
Either
qlogin -l h=compute-0-29.local
or
qlogin -q "*#compute-0-29.local"
Should do the job
Based on talking to some HPC specialists at work and some Google searching on the subject (I also wanted to resume a job ID), it's not really possible if you've already submitted the job. You can qlogin -q <node name> into the node again, but you cannot resume the job on the shell screen.
If you are thinking of starting a new qlogin job, but you would like to be able to resume it at some future point, then you can use screen to do this.
Before you write qlogin into the command line at the front-end node, write screen. It should completely clear the terminal screen.
Now qlogin and put in your job script interactively.
Once your job has started running and you want to leave for a bit, press and hold Cntl while you press A and D. It should say that your screen was detached and take you back to the front-end node. If you qstat now, you should see your job running.
When you want to resume the job ID (see the running process on the terminal screen), in the front-end node write screen -r. You should be able to see your running process in the terminal again.
Note: if you do this several times and you accumulate multiple screens by accident (happens to me every time), when you screen -r you will get multiple choices instead of automatically resuming the one you want. To try each one out, type screen -r <name of screen listed> one at a time until you find the one you want (detach as specified above). To get rid of the extra screens, write screen -D -r <name>.
Hope this helps.

Tomcat freezes after changing date and time system

I'm working on a GWT application that has a feature to change system's date and time. In order to execute the linux date command (for example: date 052810242012) the following line is executed but after that tomcat freezes:
Runtime.getRuntime().exec(this.command); // where this.command is the date command
I've realized that even if the application is up and running and I change the system date and time in a terminal, it freezes the application anyway.
Could someone explain why ?
Firstly, I think the command that you wanted to execute is a root-only command,so you must make sure that the owner of your application is a sudoer,and it has right to execute that command.
Second, Runtime.getRuntime().exec() offers an interface to execute system command for JAVA but there is a difference between executing directly on the temina because it deals with the input and output itself, and will not print on the screen like teminal so you must deal with the result of the command in the JVM or write the command into a executable shell script with no output and call
Runtime.getRuntime().exec(sudo","./a.sh"};
Finally, the problem you mention when you change the date and time in a teminal leads to freeze the application, I've never seen this before. Could you just explain its detail ?

Resources