Is it possible to see live output of command runned by execute_process? - linux

I'm running some time consuming bash script:
execute_process(
COMMAND "bash" "slow_script.sh"
WORKING_DIRECTORY ${INSTALL_SCRIPT_DIR}
ERROR_VARIABLE ERROR_MESSAGE
RESULT_VARIABLE ERROR_CODE)
and I want to see progress. I tried to show xterm window:
execute_process(
COMMAND "xterm" "-e" "slow_script.sh"
WORKING_DIRECTORY ${INSTALL_SCRIPT_DIR}
ERROR_VARIABLE ERROR_MESSAGE
RESULT_VARIABLE ERROR_CODE)
It works, but seems ugly.
Is it possible to show script output in CMake output while script is executing?

Probably you could use some of the standard /dev devices as OUTPUT_FILE.
The following CMake example worked with a quick test on my Ubuntu machine:
cmake_minimum_required(VERSION 2.4)
project(TestExecuteProcessToStdOut NONE)
execute_process(
COMMAND "${CMAKE_COMMAND}" -E echo "Test"
ERROR_VARIABLE ERROR_MESSAGE
RESULT_VARIABLE ERROR_CODE
OUTPUT_FILE "/proc/self/fd/0"
)
References
Difference between FILE * "/dev/stdout" and stdout
Unix & Linux: echo or print /dev/stdin /dev/stdout /dev/stderr

Related

Linux script How to open new terminal and run commands in it

I am writing a script which will take users input and then run following command in new terminal.
I am working on bigger "script", but for testing I created this little version of my problem.
newtermtest.sh
okegreen='\033[92m'
while true
read -p "Input Selection: " mainmenuinput
do
#find the name of instaled terminal then run new window of it and run bash in it !
#on my computer I have instaled x-terminal-emulator: so testing on it ---- NOT WORKING YET
case $mainmenuinput in
"0")
x-terminal-emulator -e "bash -c 'nmap -sV -sC 189.55.23.174 -vvv'"
;;
"1")
path=$(pwd)
x-terminal-emulator -e "bash -c 'cd $path;touch silk.txt'"
;;
*)
break;
esac
done
echo -e "${okegreen}leaving programe..."
I have found that something like x-terminal-emulator -e {command} should work, but when I run it I have the following error and the screen just flashes.
returned message:
QDir::exists: Empty or null file name
Then I would need something to keep the terminal up, I found --noclose or -hold tags, but I can't test it util I find the working command.
Do you have some experiences with opening new terminal from bash script ?
Iam opened for any hints.
Some temporary solution which works now:
x-terminal-emulator -e 'nmap -sV -sC 189.55.23.174 -vvv'
I used just -e to execute command and with nmap it is working now.
I will figure out other commands and I will try to put it in complex script with more types of terminals.
Thanks All for suggestions.

Get bash script to open terminal

In Windows when I double-click a Batch script, it will automatically open a terminal window and show me what's happening. If I were to double-click a bash script in Linux, a terminal window does not open to show me what is happening; it runs in the background. I have seen that one can use one script to launch another script in a new terminal window with x-terminal-emulator -e "./script.sh", but is there any bash command I can put into the same (one) script.sh so that it will open a terminal and show me what's happening (or if I need to answer y/n questions)?
You can do something similar to what Slax
developers do in their bootinst.sh:
#!/usr/bin/env sh
#
# If you see this file in a text editor instead of getting it executed,
# then it is missing executable permissions (chmod). You can try to set
# exec permissions for this file by using: chmod a+x bootinst.sh
#
# Scrolling down will reveal the actual code of this script.
#
# if we're running this from X, re-run the script in konsole or xterm
if [ "$DISPLAY" != "" ]; then
if [ "$1" != "--rex" -a "$2" != "--rex" ]; then
konsole --nofork -e /bin/sh $0 --rex 2>/dev/null || xterm -e /bin/sh $0 --rex 2>/dev/null || /bin/sh $0 --rex 2>/dev/null
exit
fi
fi
# put contents of your script here
echo hi
# do not close the terminal immediately, let user look at the results
echo "Press Enter..."
read junk
This script would run correctly both when started in graphical
environment and in tty. It tries to restart the script inside
konsole and xterm and but if it doesn't find neither of them it
will simply run in the background.

ssh does not return even after execution

The following ssh command does not return to terminal. It hangs though the execution is completed. The execution hangs after echo hi command.
ssh user#testserver "echo hello;source .profile;source .bash_profile;/apps/myapp/deploytools/ciInstallAndRun.sh; echo hi"
Output
hello
<outoutfrom remote script"
hi
ciInstallAndRun.sh
echo 'starting'
cd /apps/myapp/current
./tctl kill
cd /apps/myapp
mv myapp_v1.0 "myapp_v1.0_`date '+%Y%m%d%H%M'`"
unzip -o /apps/myapp/myappdist-bin.zip
java -classpath .:/apps/myapp/deploytools/cleanup.jar se.telenor.project.cleanup.Cleanup /apps/myapp myapp_v1.0_ 3
cd /apps/myapp/myapp_v1.0
echo 'Done with deploy'
chmod -R 775 *
echo 'Done'
./tctl start test
Source OS: Redhat
Dest Os: Solaris 10 8/07
Any idea to fix this.
Any idea to fix this.
Your installation script has spawned a child process.
Add a ps -f or ptree $$ command before echo hi. You'll see a child process or multiple child processes spawned by your install script.
To stop the SSH command from hanging, you need to detach such child process(es) from your terminal's input/output. You can sedirect your script's output to a file - both stdout and stderr with > /some/output/file 2>&1, and also redirect its input with < /dev/null.
Or you can use the nohup command.
You haven't provided an MCVE, as others have noted, but this is likely the problem command in you install script, since your question implies that you see the expected output from your install script:
./tctl start test
You probably would do better to replace it with something like:
./tctl start test </dev/null >/some/log/file/path.log 2>&1

Linux LVM lvs command fails from cron perl script but works from cron directly

I am trying to run "lvs" in a perl script to parse its output.
my $output = `lvs --noheadings --separator : --units m --nosuffix 2>&1`;
my $result = $?;
if ($result != 0 || length($output) == 0) {
printf STDERR "Get list of LVs failed (exit result: %d): %s\n",
$result, $output;
exit(1);
}
printf "SUCCESS:\n%s\n", $output;
When I run the above script from a terminal window it runs fine. If I run via cron it fails:
Get list of LVs failed (exit result: -1):
Note the lack of any output (stdout + stderr)
If I run the same "lvs --noheadings --separator : --units m --nosuffix" command directly in cron, it runs and outputs just fine.
If I modify the perl script to use open3() I also get the same failure with no output.
If I add "-d -d -d -d -d -v -v -v" to the lvs command to enable verbose/debug output I see that when I run the perl script from terminal, but there is no output when run via cron/perl.
I'm running this on RHEL 7.2 with /usr/bin/perl (5.16.3)
Any suggestions???
According to perldoc system, "Return value of -1 indicates a failure to start the program or an error of the wait(2) system call (inspect $! for the reason)." So the reason there's no output is because lvs isn't being started successfully.
Given the usual nature of cron-related problems, I'd say the most likely reason it's failing to run would be that it's not on the $PATH used by cron. Try specifying the full path and, if that doesn't work, check $! for the operating system's error message.
Try using absolute path to lvs:
my $output = `/sbin/lvs --noheadings --separator : --units m --nosuffix 2>&1`;

Run a shell script in new terminal from current terminal

How do you run a shell script in a new terminal in Linux from a terminal like "start test.bat" in Windows, also it should be working in the console mode.
Here's a simple example to get you started:
To write a shell script, do this on your command prompt:
echo -e '#!/bin/sh\n echo "hello world"' > abc.sh
This writes:
#!/bin/sh
echo "hello world"
To a file called abc.sh
Next, you want to set it to executable by:
chmod +x abc.sh
Now, you can run it by:
./abc.sh
And you should see:
hello world
On your terminal.
To run it in a new terminal, you can do:
gnome-terminal -x ./abc.sh
or, if it's xterm:
xterm -e ./abc.sh
Here's a list of different terminal emulators.
Alternatively, you just run it in your current terminal, but background it instead by:
./abc.sh &
I came here wanting to figure out how to make a script spawn a terminal and run it self in it, so for those who want to do that I figured out this solution:
if [ ! -t 0 ]; then # script is executed outside the terminal?
# execute the script inside a terminal window with same arguments
x-terminal-emulator -e "$0" "$#"
# and abort running the rest of it
exit 0
fi
For gnome try this.
Replace ls with the command you want to run
gnome-terminal -x sh -c "ls|less"
I hope this is what you want
As of January 2020, the -e and -x option in gnome-terminal still run properly but throw out the following warnings:
For -e:
# Option “-e” is deprecated and might be removed in a later version
of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to
execute after it.
For -x:
# Option “-x” is deprecated and might be removed in a later version
of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to
execute after it.
Based on that information above, I confirmed that you can run the following two commands without receiving any warning messages:
gnome-terminal -- /bin/sh -c '<your command>'
gnome-terminal -- ./<your script>.sh
I hope this helps anyone else presently having this issue :)

Resources