1

How to tell whether previous command was successfully carried out or not.

I know one way is to use echo $? command:

[username@hostname ~]$ echo $?
0

But what about a scenario like this: say I want to run a python script called "ROSE_main.py"

[username@hostname rose]$ python ROSE_main.py -g HG18 -i HG18_MM1S_MED1.gff -r MM1S_MED1.hg18.bwt.sorted.bam -c MM1S_WCE.hg18.bwt.sorted.bam -o example -s 12500 -t 2500
USING HG18_MM1S_MED1.gff AS THE INPUT GFF
USING HG18 AS THE GENOME
MAKING START DICT
LOADING IN GFF REGIONS
CHECKING INPUT TO MAKE SURE EACH REGION HAS A UNIQUE IDENTIFIER
REFERENCE COLLECTION PASSES QC
STITCHING REGIONS TOGETHER
PERFORMING REGION STITCHING
REMOVED 7865 LOCI BECAUSE THEY WERE CONTAINED BY A TSS

...

The command seems to be executed like forever. When I used "top" command in a new tap, here is what I see:

   315 root      RT   0     0    0    0 S 100.0  0.0   1801:10 migration/104                                                                                                                    
    42 root      RT   0     0    0    0 S 100.0  0.0   2023:48 migration/13                                                                                                                     
   177 root      RT   0     0    0    0 S 100.0  0.0   1622:39 migration/58                                                                                                                     
    51 root      RT   0     0    0    0 S 100.0  0.0   1820:09 migration/16                                                                                                                     
    66 root      RT   0     0    0    0 R 100.0  0.0   2135:56 migration/21                                                                                                                     
   462 root      RT   0     0    0    0 R 100.0  0.0   1696:18 migration/153                                                                                                                    
    63 root      RT   0     0    0    0 S 100.0  0.0   1735:34 migration/20                                                                                                                     
    54 root      RT   0     0    0    0 S 100.0  0.0   1844:20 migration/17                                                                                                                     
   429 root      RT   0     0    0    0 S 100.0  0.0   1690:33 migration/142                                                                                                                    
 11471 gdm       20   0  374m  12m 4544 R 117.1  0.0 934:51.45 gnome-settings-                                                                                                                  
   318 root      RT   0     0    0    0 S 100.0  0.0   1774:44 migration/105 

I guess if the command is still actively running, I should be able to see my username and something like "python", but instead, I saw nothing like that.

When I came to the command tap, the cursor was always flashing, so the command is still being executed, how could that be?

How could I know whether the command is still running?

top command did not give me any information. Neither could I use echo $? (if I tried to input echo $?, I will get some feedback such as pipeline broken)

  • If you got pipeline broken when you entered something on the command line, it means that the command was waiting for input from stdin. – RealSkeptic May 2 '16 at 9:48
  • Thanks, RealSkeptic. So the command is not dead. Do you think it is better to just wait? Also, is there any way to know the running condition, instead of passively waiting and praying? – Jun May 2 '16 at 9:53
  • Try using ps -ef to list all processes that are running, rather than top which just shows the top processes. – steve May 2 '16 at 9:54
  • 'Myusername 498271 464873 0 19:00 pts/33 00:00:00 ps -ef Mysusername 498361 465108 0 19:00 pts/26 00:00:00 /bin/sh -c samtools view MM1S_WCE.hg18.bwt.sorted.bam chr15:79190228-79191852 Myusername 498374 465104 0 19:00 pts/26 00:00:00 samtools view MM1S_MED1.hg18.bwt.sorted.bam chr15:44424045-44425235 myusername 498375 465106 0 19:00 pts/26 00:00:00 samtools view MM1S_MED1.hg18.bwt.sorted.bam chr5:36186618-36188852' Yes, I could see the running processes. Tsk! steve. – Jun May 2 '16 at 10:03
2

If the command died, you'd get the shell prompt back, with or without an error message.

So in any case, it is still running. But in your case, it seems to be waiting for input from the terminal. This usually means that it will never end, unless you give it the input that it is waiting for.

In any case, top is not the right tool. It is meant to show the top processes - either the ones that use most CPU, most memory, etc. - and your command isn't necessarily one of the processes that rank highest in any category that top sorts by.

The proper tool to see the status of a command is ps. You can use ps to show all the processes in the system (with -e), only your own (or a particular user's) processes (with -U), or specific processes. Other parameters to ps are defining its format. So in your case you could use

ps -lf $(pgrep python)

To see all the processes whose head command is python.

The -lf gives you a lot of information about each such process - among them the state of the process (under the column S), and if it's currently sleeping rather than running, the system call where it has stopped (WCHAN).

If the command is running, rather than waiting, you'd see R in the S column, and - in the WCHAN column. If it's waiting for something, you'd see S in the S column, and something like wait_w, poll_s, futex_ etc. in the WCHAN column.

It's hard to say what it is waiting for exactly, but if you see that it stays in that state for a long time and you were expecting it to start running, you should check the command itself to see if you ran it correctly.

Read the ps manual for further details about the possible formats and what you can see in its output.

And as for your python command, you should check to see why it is waiting for user input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.