LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 12:50 AM   #1
hack3rcon
Senior Member
 
Registered: Jan 2015
Posts: 1,432

Rep: Reputation: 11
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, 01:18 AM   #2
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,611
Blog Entries: 19

Rep: Reputation: 4458Reputation: 4458Reputation: 4458Reputation: 4458Reputation: 4458Reputation: 4458Reputation: 4458Reputation: 4458Reputation: 4458Reputation: 4458Reputation: 4458
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, 02:07 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
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, 02:59 AM   #4
malekmustaq
Senior Member
 
Registered: Dec 2008
Location: root
Distribution: Slackware & BSD
Posts: 1,669

Rep: Reputation: 498Reputation: 498Reputation: 498Reputation: 498Reputation: 498
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 10:15 PM.
 
1 members found this post helpful.
Old 08-09-2016, 08:58 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,671
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
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, 09:50 AM   #6
hack3rcon
Senior Member
 
Registered: Jan 2015
Posts: 1,432

Original Poster
Rep: Reputation: 11
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, 10:44 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,927

Rep: Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320
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, 10:22 PM   #8
malekmustaq
Senior Member
 
Registered: Dec 2008
Location: root
Distribution: Slackware & BSD
Posts: 1,669

Rep: Reputation: 498Reputation: 498Reputation: 498Reputation: 498Reputation: 498
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, 03:03 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 04:35 AM   #10
hack3rcon
Senior Member
 
Registered: Jan 2015
Posts: 1,432

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


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:45 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
Open Source Consulting | Domain Registration