LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-09-2016, 01:50 AM   #1
hack3rcon
Member
 
Registered: Jan 2015
Posts: 903

Rep: Reputation: Disabled
Post How to know last commands run successfully?


Hello.
I know that with the "echo $?" I can understand that the last command run successfully or not but how about other commands? For example, I run a command on a remote server via SSH and execute a "dd" command and close my terminal and do another SSH and run "ls" command but "echo $?" show me the result of "ls" command not "dd" command. How can I understand the result of "dd" command?

Thank you.
 
Old 08-09-2016, 02:18 AM   #2
hazel
Member
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: Debian, Crux, LFS, Dragora, NuTyX
Posts: 513

Rep: Reputation: 229Reputation: 229Reputation: 229
One way would be to keep a log file and follow each command with a conditional echo to that file.
Code:
if dd if=infile of=outfile bs=blocks; then
 echo "dd ran successfully" >> logfile
else echo "dd failed" >> logfile
fi
After all, that's basically how the init scripts do it.
 
1 members found this post helpful.
Old 08-09-2016, 03:07 AM   #3
Turbocapitalist
Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 954
Blog Entries: 3

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
You have to look for the exit code for "dd" directly. You can do that on the remote host before logging out of the server. Or, if running just a single program or script in an automated fashion, the SSH client will pass the exit code of that program or script. It will also pass the exit code of the last program or script run before "exit". So if you connect with "ssh", run "dd", and then do nothing else before exit, then you can see the exit code of "dd" with $? on the local system.

Code:
ssh server.example.com /bin/true
echo $?
ssh server.example.com /bin/false
echo $?
 
Old 08-09-2016, 03:59 AM   #4
malekmustaq
Senior Member
 
Registered: Dec 2008
Location: root
Distribution: Slackware & BSD
Posts: 1,613

Rep: Reputation: 433Reputation: 433Reputation: 433Reputation: 433Reputation: 433
Quote:
For example, I run a command on a remote server via SSH and execute a "dd" command and close my terminal and do another SSH and run "ls" command but "echo $?" show me the result of "ls" command not "dd" command. How can I understand the result of "dd" command?
By recording a transcript of all activities, outputs and command returns made or transpired in the console. To do that, after opening the console/tty you will enter the command--
Code:
script mytranscriptfile1.txt
then you may now proceed to issue commands whatever you do, ssh, dd, ls, vi, etc. After you do that console session you may check and review what returns echoed by the commands as they ran.
Code:
cat mytranscriptfile1.txt
there you can find out whether the commands went good < 0 > or went error < 1 >. You will read the record even if you were not around. Just do not exit from the console. Should you have exited or quit from script CTL+D just redo the pre-pending of "script" anew.

Hope that helps.

Good luck and enjoy.

m.m.

Last edited by malekmustaq; 08-09-2016 at 11:15 PM.
 
1 members found this post helpful.
Old 08-09-2016, 09:58 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 7,151

Rep: Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203
By tradition, Unix/Linux programs have two "standard outputs": STDOUT, which is for "normal" output, and STDERR, which is for "error" (or "status message") output.

Although normally both of these output streams are displayed to the console and intermingled there, you can redirect them separately. For instance, the following command discards the error-output of the foobar command, by directing it to "the bit-bucket" (/dev/null, while sending the standard-output to the disk-file bletch:

Code:
 foobar >bletch 2>/dev/null
If you need to determine whether a command worked, you can test the command's exit-code for zero. (Zero means "success.") If you need details about why it [didn't ...] work, you must capture and parse the command's standard-error output.
 
Old 08-09-2016, 10:50 AM   #6
hack3rcon
Member
 
Registered: Jan 2015
Posts: 903

Original Poster
Rep: Reputation: Disabled
Just "script" exist? I guess a program must be exist for run command in different terminal and switch between them via PID.
 
Old 08-09-2016, 11:44 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
I do not understand your last post at all.
To the original question see man ssh, exit status:
Code:
ssh exits with the exit status of the remote command or with 255 if an error occurred.
Therefore the first ssh will return the status of ls, the second the status of dd.
You can also save the exit status by:
Code:
ssh <some command>
RC=$?
some other commands
use $RC (if you want)
 
Old 08-09-2016, 11:22 PM   #8
malekmustaq
Senior Member
 
Registered: Dec 2008
Location: root
Distribution: Slackware & BSD
Posts: 1,613

Rep: Reputation: 433Reputation: 433Reputation: 433Reputation: 433Reputation: 433
Quote:
Originally Posted by hack3rcon View Post
Just "script" exist? I guess a program must be exist for run command in different terminal and switch between them via PID.
I forgot the basic clue, I am sorry. Just check the command and understand further:
Code:
 man script
The transcript runs in the background spawned from the console. If you want to run another transcript in separate console you may redo the method in the newly opened console.

Goodluck and enjoy!

m.m.
 
Old 08-10-2016, 04:03 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
In short you have to collect the $? status immediately after each cmd, pref in separate vars if you intend to use those values later.
 
Old 08-13-2016, 05:35 AM   #10
hack3rcon
Member
 
Registered: Jan 2015
Posts: 903

Original Poster
Rep: Reputation: Disabled
I guess a program with the name "screen" can do it
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Unable to successfully run menulibre JKostaRibeiro Slackware 15 05-21-2015 05:30 PM
cannot get tomcat to run successfully - various errors nullp0inter Linux - Software 14 08-23-2009 05:24 PM
Why only root can successfully run startx alxnihon Mandriva 4 04-27-2004 09:55 AM
Who can successfully run Azureus ? I really go mad.... preswang Linux - Software 6 04-07-2004 07:51 AM
Anyone run endnote using wine successfully? moonsun Linux - Software 0 05-01-2003 10:56 AM


All times are GMT -5. The time now is 10:39 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration