Is it possible to check if a specific process is sleeping or running?












4















I've created the following script on Ubuntu that can pause and start a specific process:



#!/bin/bash

loopProcess () {
COUNTER=0
while [ true ]; do
echo $COUNTER
sleep 1
let COUNTER=COUNTER+1
done
}

loopProcess &
pidLoopProcess=$(echo $!)

while [ true ]; do
read -p "" state
if [ "$state" == 'a' ]; then
echo "Process is running"
kill -CONT "$pidLoopProcess"
elif [ "$state" == 'b' ]; then
echo "Process is sleeping"
kill -STOP "$pidLoopProcess"
fi
done


Demonstration of how it works:



enter image description here



I'd like to know if it's possible to check when a specific process is running or sleeping using the command line. The pseudocode would be something like this:



if [ "$(StatePID $pidLoopProcess)" == 'sleeping'  ]; then
## do something
fi


I know that with this script I could just declare some global variables and use them as flags... But I want to know if there's a command line tool that does that for me. Is there? Is it possible?










share|improve this question




















  • 1





    using the ps command?

    – Rui F Ribeiro
    Dec 31 '18 at 13:53






  • 2





    What operating system are you using? And do you need solutions for that operating system only or do solutions need to be portable?

    – terdon
    Dec 31 '18 at 14:37











  • @terdon I'm using Ubuntu... I've edited the tags of my question to specify that.

    – Rafael Muynarsk
    Dec 31 '18 at 15:00






  • 1





    Thanks, but please don't use tags for that. OS tags should only be used when the question is asking about something specific to that OS and not to indicate that you are just using that OS. By the way, your pidLoopProcess=$(echo $!) would be better written as pidLoopProcess=$!. Also, be aware that since you are using sleep here, your status will very often be S since the ps will most likely catch the loopProcess while the sleep is being run.

    – terdon
    Dec 31 '18 at 15:14
















4















I've created the following script on Ubuntu that can pause and start a specific process:



#!/bin/bash

loopProcess () {
COUNTER=0
while [ true ]; do
echo $COUNTER
sleep 1
let COUNTER=COUNTER+1
done
}

loopProcess &
pidLoopProcess=$(echo $!)

while [ true ]; do
read -p "" state
if [ "$state" == 'a' ]; then
echo "Process is running"
kill -CONT "$pidLoopProcess"
elif [ "$state" == 'b' ]; then
echo "Process is sleeping"
kill -STOP "$pidLoopProcess"
fi
done


Demonstration of how it works:



enter image description here



I'd like to know if it's possible to check when a specific process is running or sleeping using the command line. The pseudocode would be something like this:



if [ "$(StatePID $pidLoopProcess)" == 'sleeping'  ]; then
## do something
fi


I know that with this script I could just declare some global variables and use them as flags... But I want to know if there's a command line tool that does that for me. Is there? Is it possible?










share|improve this question




















  • 1





    using the ps command?

    – Rui F Ribeiro
    Dec 31 '18 at 13:53






  • 2





    What operating system are you using? And do you need solutions for that operating system only or do solutions need to be portable?

    – terdon
    Dec 31 '18 at 14:37











  • @terdon I'm using Ubuntu... I've edited the tags of my question to specify that.

    – Rafael Muynarsk
    Dec 31 '18 at 15:00






  • 1





    Thanks, but please don't use tags for that. OS tags should only be used when the question is asking about something specific to that OS and not to indicate that you are just using that OS. By the way, your pidLoopProcess=$(echo $!) would be better written as pidLoopProcess=$!. Also, be aware that since you are using sleep here, your status will very often be S since the ps will most likely catch the loopProcess while the sleep is being run.

    – terdon
    Dec 31 '18 at 15:14














4












4








4








I've created the following script on Ubuntu that can pause and start a specific process:



#!/bin/bash

loopProcess () {
COUNTER=0
while [ true ]; do
echo $COUNTER
sleep 1
let COUNTER=COUNTER+1
done
}

loopProcess &
pidLoopProcess=$(echo $!)

while [ true ]; do
read -p "" state
if [ "$state" == 'a' ]; then
echo "Process is running"
kill -CONT "$pidLoopProcess"
elif [ "$state" == 'b' ]; then
echo "Process is sleeping"
kill -STOP "$pidLoopProcess"
fi
done


Demonstration of how it works:



enter image description here



I'd like to know if it's possible to check when a specific process is running or sleeping using the command line. The pseudocode would be something like this:



if [ "$(StatePID $pidLoopProcess)" == 'sleeping'  ]; then
## do something
fi


I know that with this script I could just declare some global variables and use them as flags... But I want to know if there's a command line tool that does that for me. Is there? Is it possible?










share|improve this question
















I've created the following script on Ubuntu that can pause and start a specific process:



#!/bin/bash

loopProcess () {
COUNTER=0
while [ true ]; do
echo $COUNTER
sleep 1
let COUNTER=COUNTER+1
done
}

loopProcess &
pidLoopProcess=$(echo $!)

while [ true ]; do
read -p "" state
if [ "$state" == 'a' ]; then
echo "Process is running"
kill -CONT "$pidLoopProcess"
elif [ "$state" == 'b' ]; then
echo "Process is sleeping"
kill -STOP "$pidLoopProcess"
fi
done


Demonstration of how it works:



enter image description here



I'd like to know if it's possible to check when a specific process is running or sleeping using the command line. The pseudocode would be something like this:



if [ "$(StatePID $pidLoopProcess)" == 'sleeping'  ]; then
## do something
fi


I know that with this script I could just declare some global variables and use them as flags... But I want to know if there's a command line tool that does that for me. Is there? Is it possible?







bash command-line process






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 31 '18 at 15:22







Rafael Muynarsk

















asked Dec 31 '18 at 13:51









Rafael MuynarskRafael Muynarsk

427616




427616








  • 1





    using the ps command?

    – Rui F Ribeiro
    Dec 31 '18 at 13:53






  • 2





    What operating system are you using? And do you need solutions for that operating system only or do solutions need to be portable?

    – terdon
    Dec 31 '18 at 14:37











  • @terdon I'm using Ubuntu... I've edited the tags of my question to specify that.

    – Rafael Muynarsk
    Dec 31 '18 at 15:00






  • 1





    Thanks, but please don't use tags for that. OS tags should only be used when the question is asking about something specific to that OS and not to indicate that you are just using that OS. By the way, your pidLoopProcess=$(echo $!) would be better written as pidLoopProcess=$!. Also, be aware that since you are using sleep here, your status will very often be S since the ps will most likely catch the loopProcess while the sleep is being run.

    – terdon
    Dec 31 '18 at 15:14














  • 1





    using the ps command?

    – Rui F Ribeiro
    Dec 31 '18 at 13:53






  • 2





    What operating system are you using? And do you need solutions for that operating system only or do solutions need to be portable?

    – terdon
    Dec 31 '18 at 14:37











  • @terdon I'm using Ubuntu... I've edited the tags of my question to specify that.

    – Rafael Muynarsk
    Dec 31 '18 at 15:00






  • 1





    Thanks, but please don't use tags for that. OS tags should only be used when the question is asking about something specific to that OS and not to indicate that you are just using that OS. By the way, your pidLoopProcess=$(echo $!) would be better written as pidLoopProcess=$!. Also, be aware that since you are using sleep here, your status will very often be S since the ps will most likely catch the loopProcess while the sleep is being run.

    – terdon
    Dec 31 '18 at 15:14








1




1





using the ps command?

– Rui F Ribeiro
Dec 31 '18 at 13:53





using the ps command?

– Rui F Ribeiro
Dec 31 '18 at 13:53




2




2





What operating system are you using? And do you need solutions for that operating system only or do solutions need to be portable?

– terdon
Dec 31 '18 at 14:37





What operating system are you using? And do you need solutions for that operating system only or do solutions need to be portable?

– terdon
Dec 31 '18 at 14:37













@terdon I'm using Ubuntu... I've edited the tags of my question to specify that.

– Rafael Muynarsk
Dec 31 '18 at 15:00





@terdon I'm using Ubuntu... I've edited the tags of my question to specify that.

– Rafael Muynarsk
Dec 31 '18 at 15:00




1




1





Thanks, but please don't use tags for that. OS tags should only be used when the question is asking about something specific to that OS and not to indicate that you are just using that OS. By the way, your pidLoopProcess=$(echo $!) would be better written as pidLoopProcess=$!. Also, be aware that since you are using sleep here, your status will very often be S since the ps will most likely catch the loopProcess while the sleep is being run.

– terdon
Dec 31 '18 at 15:14





Thanks, but please don't use tags for that. OS tags should only be used when the question is asking about something specific to that OS and not to indicate that you are just using that OS. By the way, your pidLoopProcess=$(echo $!) would be better written as pidLoopProcess=$!. Also, be aware that since you are using sleep here, your status will very often be S since the ps will most likely catch the loopProcess while the sleep is being run.

– terdon
Dec 31 '18 at 15:14










1 Answer
1






active

oldest

votes


















10














On Linux, you can use this to get the status of a process with a given PID:



ps -o stat= $pid


That returns T when a process is stopped. So, assuming you are on a Linux system, you could do something like this:



if [ "$(ps -o stat= $pid)" = "T" ]; then 
echo stopped
else
echo not stopped
fi


A full list of process state codes is given in man ps:



PROCESS STATE CODES
Here are the different values that the s, stat and state output specifiers
(header "STAT" or "S") will display to describe the state of a process:

D uninterruptible sleep (usually IO)
I Idle kernel thread
R running or runnable (on run queue)
S interruptible sleep (waiting for an event to complete)
T stopped by job control signal
t stopped by debugger during the tracing
W paging (not valid since the 2.6.xx kernel)
X dead (should never be seen)
Z defunct ("zombie") process, terminated but not reaped by its parent

For BSD formats and when the stat keyword is used, additional characters may be
displayed:

< high-priority (not nice to other users)
N low-priority (nice to other users)
L has pages locked into memory (for real-time and custom IO)
s is a session leader
l is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
+ is in the foreground process group





share|improve this answer
























  • Thanks, that solved my problem!! But in the context of my script the states are T+ and S+ because it's a foreground process..

    – Rafael Muynarsk
    Dec 31 '18 at 14:56













  • perhaps case ... in (T*)... then?

    – Jeff Schaller
    Dec 31 '18 at 15:00






  • 1





    @RafaelMuynarsk well, you were running it in the background in your example. Also, no, the comparison will work with =. In fact, = is more portable than == which is a bashism. See help test which doesn't even mention the ==.

    – terdon
    Dec 31 '18 at 15:01











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f491764%2fis-it-possible-to-check-if-a-specific-process-is-sleeping-or-running%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









10














On Linux, you can use this to get the status of a process with a given PID:



ps -o stat= $pid


That returns T when a process is stopped. So, assuming you are on a Linux system, you could do something like this:



if [ "$(ps -o stat= $pid)" = "T" ]; then 
echo stopped
else
echo not stopped
fi


A full list of process state codes is given in man ps:



PROCESS STATE CODES
Here are the different values that the s, stat and state output specifiers
(header "STAT" or "S") will display to describe the state of a process:

D uninterruptible sleep (usually IO)
I Idle kernel thread
R running or runnable (on run queue)
S interruptible sleep (waiting for an event to complete)
T stopped by job control signal
t stopped by debugger during the tracing
W paging (not valid since the 2.6.xx kernel)
X dead (should never be seen)
Z defunct ("zombie") process, terminated but not reaped by its parent

For BSD formats and when the stat keyword is used, additional characters may be
displayed:

< high-priority (not nice to other users)
N low-priority (nice to other users)
L has pages locked into memory (for real-time and custom IO)
s is a session leader
l is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
+ is in the foreground process group





share|improve this answer
























  • Thanks, that solved my problem!! But in the context of my script the states are T+ and S+ because it's a foreground process..

    – Rafael Muynarsk
    Dec 31 '18 at 14:56













  • perhaps case ... in (T*)... then?

    – Jeff Schaller
    Dec 31 '18 at 15:00






  • 1





    @RafaelMuynarsk well, you were running it in the background in your example. Also, no, the comparison will work with =. In fact, = is more portable than == which is a bashism. See help test which doesn't even mention the ==.

    – terdon
    Dec 31 '18 at 15:01
















10














On Linux, you can use this to get the status of a process with a given PID:



ps -o stat= $pid


That returns T when a process is stopped. So, assuming you are on a Linux system, you could do something like this:



if [ "$(ps -o stat= $pid)" = "T" ]; then 
echo stopped
else
echo not stopped
fi


A full list of process state codes is given in man ps:



PROCESS STATE CODES
Here are the different values that the s, stat and state output specifiers
(header "STAT" or "S") will display to describe the state of a process:

D uninterruptible sleep (usually IO)
I Idle kernel thread
R running or runnable (on run queue)
S interruptible sleep (waiting for an event to complete)
T stopped by job control signal
t stopped by debugger during the tracing
W paging (not valid since the 2.6.xx kernel)
X dead (should never be seen)
Z defunct ("zombie") process, terminated but not reaped by its parent

For BSD formats and when the stat keyword is used, additional characters may be
displayed:

< high-priority (not nice to other users)
N low-priority (nice to other users)
L has pages locked into memory (for real-time and custom IO)
s is a session leader
l is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
+ is in the foreground process group





share|improve this answer
























  • Thanks, that solved my problem!! But in the context of my script the states are T+ and S+ because it's a foreground process..

    – Rafael Muynarsk
    Dec 31 '18 at 14:56













  • perhaps case ... in (T*)... then?

    – Jeff Schaller
    Dec 31 '18 at 15:00






  • 1





    @RafaelMuynarsk well, you were running it in the background in your example. Also, no, the comparison will work with =. In fact, = is more portable than == which is a bashism. See help test which doesn't even mention the ==.

    – terdon
    Dec 31 '18 at 15:01














10












10








10







On Linux, you can use this to get the status of a process with a given PID:



ps -o stat= $pid


That returns T when a process is stopped. So, assuming you are on a Linux system, you could do something like this:



if [ "$(ps -o stat= $pid)" = "T" ]; then 
echo stopped
else
echo not stopped
fi


A full list of process state codes is given in man ps:



PROCESS STATE CODES
Here are the different values that the s, stat and state output specifiers
(header "STAT" or "S") will display to describe the state of a process:

D uninterruptible sleep (usually IO)
I Idle kernel thread
R running or runnable (on run queue)
S interruptible sleep (waiting for an event to complete)
T stopped by job control signal
t stopped by debugger during the tracing
W paging (not valid since the 2.6.xx kernel)
X dead (should never be seen)
Z defunct ("zombie") process, terminated but not reaped by its parent

For BSD formats and when the stat keyword is used, additional characters may be
displayed:

< high-priority (not nice to other users)
N low-priority (nice to other users)
L has pages locked into memory (for real-time and custom IO)
s is a session leader
l is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
+ is in the foreground process group





share|improve this answer













On Linux, you can use this to get the status of a process with a given PID:



ps -o stat= $pid


That returns T when a process is stopped. So, assuming you are on a Linux system, you could do something like this:



if [ "$(ps -o stat= $pid)" = "T" ]; then 
echo stopped
else
echo not stopped
fi


A full list of process state codes is given in man ps:



PROCESS STATE CODES
Here are the different values that the s, stat and state output specifiers
(header "STAT" or "S") will display to describe the state of a process:

D uninterruptible sleep (usually IO)
I Idle kernel thread
R running or runnable (on run queue)
S interruptible sleep (waiting for an event to complete)
T stopped by job control signal
t stopped by debugger during the tracing
W paging (not valid since the 2.6.xx kernel)
X dead (should never be seen)
Z defunct ("zombie") process, terminated but not reaped by its parent

For BSD formats and when the stat keyword is used, additional characters may be
displayed:

< high-priority (not nice to other users)
N low-priority (nice to other users)
L has pages locked into memory (for real-time and custom IO)
s is a session leader
l is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
+ is in the foreground process group






share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 31 '18 at 14:45









terdonterdon

130k32255433




130k32255433













  • Thanks, that solved my problem!! But in the context of my script the states are T+ and S+ because it's a foreground process..

    – Rafael Muynarsk
    Dec 31 '18 at 14:56













  • perhaps case ... in (T*)... then?

    – Jeff Schaller
    Dec 31 '18 at 15:00






  • 1





    @RafaelMuynarsk well, you were running it in the background in your example. Also, no, the comparison will work with =. In fact, = is more portable than == which is a bashism. See help test which doesn't even mention the ==.

    – terdon
    Dec 31 '18 at 15:01



















  • Thanks, that solved my problem!! But in the context of my script the states are T+ and S+ because it's a foreground process..

    – Rafael Muynarsk
    Dec 31 '18 at 14:56













  • perhaps case ... in (T*)... then?

    – Jeff Schaller
    Dec 31 '18 at 15:00






  • 1





    @RafaelMuynarsk well, you were running it in the background in your example. Also, no, the comparison will work with =. In fact, = is more portable than == which is a bashism. See help test which doesn't even mention the ==.

    – terdon
    Dec 31 '18 at 15:01

















Thanks, that solved my problem!! But in the context of my script the states are T+ and S+ because it's a foreground process..

– Rafael Muynarsk
Dec 31 '18 at 14:56







Thanks, that solved my problem!! But in the context of my script the states are T+ and S+ because it's a foreground process..

– Rafael Muynarsk
Dec 31 '18 at 14:56















perhaps case ... in (T*)... then?

– Jeff Schaller
Dec 31 '18 at 15:00





perhaps case ... in (T*)... then?

– Jeff Schaller
Dec 31 '18 at 15:00




1




1





@RafaelMuynarsk well, you were running it in the background in your example. Also, no, the comparison will work with =. In fact, = is more portable than == which is a bashism. See help test which doesn't even mention the ==.

– terdon
Dec 31 '18 at 15:01





@RafaelMuynarsk well, you were running it in the background in your example. Also, no, the comparison will work with =. In fact, = is more portable than == which is a bashism. See help test which doesn't even mention the ==.

– terdon
Dec 31 '18 at 15:01


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f491764%2fis-it-possible-to-check-if-a-specific-process-is-sleeping-or-running%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas