Unix - ps to check if script running2019 Community Moderator ElectionHow to check how long a process has been running?Check for process already running in webfaction?add “check if fetchmail is running” to this scriptCheck for Process if Same is RunningCheck that fail2ban is running?Peculiar Solaris integer comparison behaviourTelnet check in bash scriptCheck for process and kill if runningHow to check the shell of a running process?How can I check if a process is running a shell script?

Best approach to update all entries in a list that is paginated?

Tricky AM-GM inequality

How to share a mine of irresistable resources in the middle of an ocean without conflict?

GPLv2 - licensing for commercial use

Intuition behind counterexample of Euler's sum of powers conjecture

Is there an equal sign with wider gap?

What wound would be of little consequence to a biped but terrible for a quadruped?

Does splitting a potentially monolithic application into several smaller ones help prevent bugs?

Why the color red for the Republican Party

Why is this plane circling around the LKO airport every day?

Making a sword in the stone, in a medieval world without magic

How to create a hard link to an inode (ext4)?

Why does the negative sign arise in this thermodynamic relation?

My story is written in English, but is set in my home country. What language should I use for the dialogue?

In the late 1940’s to early 1950’s what technology was available that could melt a LOT of ice?

Should I tell my boss the work he did was worthless

How to to redirect a form to a certain node for anonymous users?

Signed and unsigned numbers

Do items de-spawn in Diablo?

How do I deal with a powergamer in a game full of beginners in a school club?

Leftbar without indentation

Am I not good enough for you?

Why doesn't this Google Translate ad use the word "Translation" instead of "Translate"?

Force user to remove USB token



Unix - ps to check if script running



2019 Community Moderator ElectionHow to check how long a process has been running?Check for process already running in webfaction?add “check if fetchmail is running” to this scriptCheck for Process if Same is RunningCheck that fail2ban is running?Peculiar Solaris integer comparison behaviourTelnet check in bash scriptCheck for process and kill if runningHow to check the shell of a running process?How can I check if a process is running a shell script?










0















I'm running a script which is meant to process a few utilities in order to recover some data. This script is situated in a directory where the users can place their file that needs to be recovered. Since many users can have access to this directory and run the script, I want to have a condition that checks if the script is already running. If yes, then echo a "try again later" message; otherwise, run as usual.



I did some research on here and figured this to be a working code for my functionality as described above.



#!/bin/bash 
me="$(basename "$0")";
running=$(ps h -C "$me" | grep -wv $$ | wc -l);
[[ $running > 1 ]] && exit;


I would like to implement this functionality and the code above in a Solaris SunOS 5.8. I understand that ps functions differently in Unix and Linux.










share|improve this question
















bumped to the homepage by Community 1 hour ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • A sure shot way would be to create lock file at the beginning of the script and delete it at the end. When the script is invoked and the lock file is present, it means that the script is still running. This will also take care of cases where the script might be invoked from multiple machines.

    – amisax
    May 8 '18 at 9:12












  • If you're decide to use a lock file mechanism as @amisax describes, you need to add trap handlers to your script to clean up the lock file if a user kills a running instance with CTRL-C or similar. And you'll also likely have to deal with cleaning up after users either kill -9 ... a running script or remove the lock file themselves and try running another instance of the script.

    – Andrew Henle
    May 8 '18 at 9:48











  • I implemented according to what amisax has suggested, works and is the easiest approach I believe. Thanks! I will indeed add trap handlers. Thanks for your comment, not something I would have thought of on my own @Andrew

    – ratcat
    May 9 '18 at 7:03















0















I'm running a script which is meant to process a few utilities in order to recover some data. This script is situated in a directory where the users can place their file that needs to be recovered. Since many users can have access to this directory and run the script, I want to have a condition that checks if the script is already running. If yes, then echo a "try again later" message; otherwise, run as usual.



I did some research on here and figured this to be a working code for my functionality as described above.



#!/bin/bash 
me="$(basename "$0")";
running=$(ps h -C "$me" | grep -wv $$ | wc -l);
[[ $running > 1 ]] && exit;


I would like to implement this functionality and the code above in a Solaris SunOS 5.8. I understand that ps functions differently in Unix and Linux.










share|improve this question
















bumped to the homepage by Community 1 hour ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • A sure shot way would be to create lock file at the beginning of the script and delete it at the end. When the script is invoked and the lock file is present, it means that the script is still running. This will also take care of cases where the script might be invoked from multiple machines.

    – amisax
    May 8 '18 at 9:12












  • If you're decide to use a lock file mechanism as @amisax describes, you need to add trap handlers to your script to clean up the lock file if a user kills a running instance with CTRL-C or similar. And you'll also likely have to deal with cleaning up after users either kill -9 ... a running script or remove the lock file themselves and try running another instance of the script.

    – Andrew Henle
    May 8 '18 at 9:48











  • I implemented according to what amisax has suggested, works and is the easiest approach I believe. Thanks! I will indeed add trap handlers. Thanks for your comment, not something I would have thought of on my own @Andrew

    – ratcat
    May 9 '18 at 7:03













0












0








0








I'm running a script which is meant to process a few utilities in order to recover some data. This script is situated in a directory where the users can place their file that needs to be recovered. Since many users can have access to this directory and run the script, I want to have a condition that checks if the script is already running. If yes, then echo a "try again later" message; otherwise, run as usual.



I did some research on here and figured this to be a working code for my functionality as described above.



#!/bin/bash 
me="$(basename "$0")";
running=$(ps h -C "$me" | grep -wv $$ | wc -l);
[[ $running > 1 ]] && exit;


I would like to implement this functionality and the code above in a Solaris SunOS 5.8. I understand that ps functions differently in Unix and Linux.










share|improve this question
















I'm running a script which is meant to process a few utilities in order to recover some data. This script is situated in a directory where the users can place their file that needs to be recovered. Since many users can have access to this directory and run the script, I want to have a condition that checks if the script is already running. If yes, then echo a "try again later" message; otherwise, run as usual.



I did some research on here and figured this to be a working code for my functionality as described above.



#!/bin/bash 
me="$(basename "$0")";
running=$(ps h -C "$me" | grep -wv $$ | wc -l);
[[ $running > 1 ]] && exit;


I would like to implement this functionality and the code above in a Solaris SunOS 5.8. I understand that ps functions differently in Unix and Linux.







process solaris






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 8 '18 at 10:03









Jeff Schaller

43.3k1159139




43.3k1159139










asked May 8 '18 at 9:05









ratcatratcat

12




12





bumped to the homepage by Community 1 hour ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 1 hour ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • A sure shot way would be to create lock file at the beginning of the script and delete it at the end. When the script is invoked and the lock file is present, it means that the script is still running. This will also take care of cases where the script might be invoked from multiple machines.

    – amisax
    May 8 '18 at 9:12












  • If you're decide to use a lock file mechanism as @amisax describes, you need to add trap handlers to your script to clean up the lock file if a user kills a running instance with CTRL-C or similar. And you'll also likely have to deal with cleaning up after users either kill -9 ... a running script or remove the lock file themselves and try running another instance of the script.

    – Andrew Henle
    May 8 '18 at 9:48











  • I implemented according to what amisax has suggested, works and is the easiest approach I believe. Thanks! I will indeed add trap handlers. Thanks for your comment, not something I would have thought of on my own @Andrew

    – ratcat
    May 9 '18 at 7:03

















  • A sure shot way would be to create lock file at the beginning of the script and delete it at the end. When the script is invoked and the lock file is present, it means that the script is still running. This will also take care of cases where the script might be invoked from multiple machines.

    – amisax
    May 8 '18 at 9:12












  • If you're decide to use a lock file mechanism as @amisax describes, you need to add trap handlers to your script to clean up the lock file if a user kills a running instance with CTRL-C or similar. And you'll also likely have to deal with cleaning up after users either kill -9 ... a running script or remove the lock file themselves and try running another instance of the script.

    – Andrew Henle
    May 8 '18 at 9:48











  • I implemented according to what amisax has suggested, works and is the easiest approach I believe. Thanks! I will indeed add trap handlers. Thanks for your comment, not something I would have thought of on my own @Andrew

    – ratcat
    May 9 '18 at 7:03
















A sure shot way would be to create lock file at the beginning of the script and delete it at the end. When the script is invoked and the lock file is present, it means that the script is still running. This will also take care of cases where the script might be invoked from multiple machines.

– amisax
May 8 '18 at 9:12






A sure shot way would be to create lock file at the beginning of the script and delete it at the end. When the script is invoked and the lock file is present, it means that the script is still running. This will also take care of cases where the script might be invoked from multiple machines.

– amisax
May 8 '18 at 9:12














If you're decide to use a lock file mechanism as @amisax describes, you need to add trap handlers to your script to clean up the lock file if a user kills a running instance with CTRL-C or similar. And you'll also likely have to deal with cleaning up after users either kill -9 ... a running script or remove the lock file themselves and try running another instance of the script.

– Andrew Henle
May 8 '18 at 9:48





If you're decide to use a lock file mechanism as @amisax describes, you need to add trap handlers to your script to clean up the lock file if a user kills a running instance with CTRL-C or similar. And you'll also likely have to deal with cleaning up after users either kill -9 ... a running script or remove the lock file themselves and try running another instance of the script.

– Andrew Henle
May 8 '18 at 9:48













I implemented according to what amisax has suggested, works and is the easiest approach I believe. Thanks! I will indeed add trap handlers. Thanks for your comment, not something I would have thought of on my own @Andrew

– ratcat
May 9 '18 at 7:03





I implemented according to what amisax has suggested, works and is the easiest approach I believe. Thanks! I will indeed add trap handlers. Thanks for your comment, not something I would have thought of on my own @Andrew

– ratcat
May 9 '18 at 7:03










1 Answer
1






active

oldest

votes


















0














The ps command you are using is not related to UNIX.



Using key letters instead of options was used in BSD ps but that did never make it into the standard. BSD even uses the h key letter in a different way as you might expect.



I recommend to use:



ps -p $$


If you like to check whether there are more instances of this script, use:



myname=`basename $0`
pids=`pgrep -d, $myname`
ps -p $pids


In theory, you do not even need to run ps anymore, it would be sufficient to check the output of pgrep for a comma.



If you like to implement a lock file algorithm and have a POSIX compliant shell, you may use:



set -o noclobber
:> /var/tmp/myscript
if [ $? -ne 0 ]; then
echo already running
exit 1
fi
... do some stuff
rm /var/tmp/myscript


Whether this works reliably depends on whether the shell implements noclobber the way POSIX likes to see it. Some shells may not implement this atomically, so you need to know whether your shell is OK.






share|improve this answer




















  • 2





    Saying to use ps -p $$ means to see if the current process (pid) is still running. It's my interpretation that the OP wants to see if a separate copy of the same script is running.

    – Jeff Schaller
    May 8 '18 at 10:04











  • Indeed my intention is as mentioned by Jeff. I want to check if the script is being run by some other user remotely elsewhere to avoid overwriting of data.

    – ratcat
    May 9 '18 at 0:55











  • So my updated answer should be sufficient for you...

    – schily
    May 9 '18 at 10:27










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%2f442493%2funix-ps-to-check-if-script-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









0














The ps command you are using is not related to UNIX.



Using key letters instead of options was used in BSD ps but that did never make it into the standard. BSD even uses the h key letter in a different way as you might expect.



I recommend to use:



ps -p $$


If you like to check whether there are more instances of this script, use:



myname=`basename $0`
pids=`pgrep -d, $myname`
ps -p $pids


In theory, you do not even need to run ps anymore, it would be sufficient to check the output of pgrep for a comma.



If you like to implement a lock file algorithm and have a POSIX compliant shell, you may use:



set -o noclobber
:> /var/tmp/myscript
if [ $? -ne 0 ]; then
echo already running
exit 1
fi
... do some stuff
rm /var/tmp/myscript


Whether this works reliably depends on whether the shell implements noclobber the way POSIX likes to see it. Some shells may not implement this atomically, so you need to know whether your shell is OK.






share|improve this answer




















  • 2





    Saying to use ps -p $$ means to see if the current process (pid) is still running. It's my interpretation that the OP wants to see if a separate copy of the same script is running.

    – Jeff Schaller
    May 8 '18 at 10:04











  • Indeed my intention is as mentioned by Jeff. I want to check if the script is being run by some other user remotely elsewhere to avoid overwriting of data.

    – ratcat
    May 9 '18 at 0:55











  • So my updated answer should be sufficient for you...

    – schily
    May 9 '18 at 10:27















0














The ps command you are using is not related to UNIX.



Using key letters instead of options was used in BSD ps but that did never make it into the standard. BSD even uses the h key letter in a different way as you might expect.



I recommend to use:



ps -p $$


If you like to check whether there are more instances of this script, use:



myname=`basename $0`
pids=`pgrep -d, $myname`
ps -p $pids


In theory, you do not even need to run ps anymore, it would be sufficient to check the output of pgrep for a comma.



If you like to implement a lock file algorithm and have a POSIX compliant shell, you may use:



set -o noclobber
:> /var/tmp/myscript
if [ $? -ne 0 ]; then
echo already running
exit 1
fi
... do some stuff
rm /var/tmp/myscript


Whether this works reliably depends on whether the shell implements noclobber the way POSIX likes to see it. Some shells may not implement this atomically, so you need to know whether your shell is OK.






share|improve this answer




















  • 2





    Saying to use ps -p $$ means to see if the current process (pid) is still running. It's my interpretation that the OP wants to see if a separate copy of the same script is running.

    – Jeff Schaller
    May 8 '18 at 10:04











  • Indeed my intention is as mentioned by Jeff. I want to check if the script is being run by some other user remotely elsewhere to avoid overwriting of data.

    – ratcat
    May 9 '18 at 0:55











  • So my updated answer should be sufficient for you...

    – schily
    May 9 '18 at 10:27













0












0








0







The ps command you are using is not related to UNIX.



Using key letters instead of options was used in BSD ps but that did never make it into the standard. BSD even uses the h key letter in a different way as you might expect.



I recommend to use:



ps -p $$


If you like to check whether there are more instances of this script, use:



myname=`basename $0`
pids=`pgrep -d, $myname`
ps -p $pids


In theory, you do not even need to run ps anymore, it would be sufficient to check the output of pgrep for a comma.



If you like to implement a lock file algorithm and have a POSIX compliant shell, you may use:



set -o noclobber
:> /var/tmp/myscript
if [ $? -ne 0 ]; then
echo already running
exit 1
fi
... do some stuff
rm /var/tmp/myscript


Whether this works reliably depends on whether the shell implements noclobber the way POSIX likes to see it. Some shells may not implement this atomically, so you need to know whether your shell is OK.






share|improve this answer















The ps command you are using is not related to UNIX.



Using key letters instead of options was used in BSD ps but that did never make it into the standard. BSD even uses the h key letter in a different way as you might expect.



I recommend to use:



ps -p $$


If you like to check whether there are more instances of this script, use:



myname=`basename $0`
pids=`pgrep -d, $myname`
ps -p $pids


In theory, you do not even need to run ps anymore, it would be sufficient to check the output of pgrep for a comma.



If you like to implement a lock file algorithm and have a POSIX compliant shell, you may use:



set -o noclobber
:> /var/tmp/myscript
if [ $? -ne 0 ]; then
echo already running
exit 1
fi
... do some stuff
rm /var/tmp/myscript


Whether this works reliably depends on whether the shell implements noclobber the way POSIX likes to see it. Some shells may not implement this atomically, so you need to know whether your shell is OK.







share|improve this answer














share|improve this answer



share|improve this answer








edited May 9 '18 at 10:29

























answered May 8 '18 at 9:36









schilyschily

10.8k31642




10.8k31642







  • 2





    Saying to use ps -p $$ means to see if the current process (pid) is still running. It's my interpretation that the OP wants to see if a separate copy of the same script is running.

    – Jeff Schaller
    May 8 '18 at 10:04











  • Indeed my intention is as mentioned by Jeff. I want to check if the script is being run by some other user remotely elsewhere to avoid overwriting of data.

    – ratcat
    May 9 '18 at 0:55











  • So my updated answer should be sufficient for you...

    – schily
    May 9 '18 at 10:27












  • 2





    Saying to use ps -p $$ means to see if the current process (pid) is still running. It's my interpretation that the OP wants to see if a separate copy of the same script is running.

    – Jeff Schaller
    May 8 '18 at 10:04











  • Indeed my intention is as mentioned by Jeff. I want to check if the script is being run by some other user remotely elsewhere to avoid overwriting of data.

    – ratcat
    May 9 '18 at 0:55











  • So my updated answer should be sufficient for you...

    – schily
    May 9 '18 at 10:27







2




2





Saying to use ps -p $$ means to see if the current process (pid) is still running. It's my interpretation that the OP wants to see if a separate copy of the same script is running.

– Jeff Schaller
May 8 '18 at 10:04





Saying to use ps -p $$ means to see if the current process (pid) is still running. It's my interpretation that the OP wants to see if a separate copy of the same script is running.

– Jeff Schaller
May 8 '18 at 10:04













Indeed my intention is as mentioned by Jeff. I want to check if the script is being run by some other user remotely elsewhere to avoid overwriting of data.

– ratcat
May 9 '18 at 0:55





Indeed my intention is as mentioned by Jeff. I want to check if the script is being run by some other user remotely elsewhere to avoid overwriting of data.

– ratcat
May 9 '18 at 0:55













So my updated answer should be sufficient for you...

– schily
May 9 '18 at 10:27





So my updated answer should be sufficient for you...

– schily
May 9 '18 at 10:27

















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%2f442493%2funix-ps-to-check-if-script-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







-process, solaris

Popular posts from this blog

Mobil Contents History Mobil brands Former Mobil brands Lukoil transaction Mobil UK Mobil Australia Mobil New Zealand Mobil Greece Mobil in Japan Mobil in Canada Mobil Egypt See also References External links Navigation menuwww.mobil.com"Mobil Corporation"the original"Our Houston campus""Business & Finance: Socony-Vacuum Corp.""Popular Mechanics""Lubrite Technologies""Exxon Mobil campus 'clearly happening'""Toledo Blade - Google News Archive Search""The Lion and the Moose - How 2 Executives Pulled off the Biggest Merger Ever""ExxonMobil Press Release""Lubricants""Archived copy"the original"Mobil 1™ and Mobil Super™ motor oil and synthetic motor oil - Mobil™ Motor Oils""Mobil Delvac""Mobil Industrial website""The State of Competition in Gasoline Marketing: The Effects of Refiner Operations at Retail""Mobil Travel Guide to become Forbes Travel Guide""Hotel Rankings: Forbes Merges with Mobil"the original"Jamieson oil industry history""Mobil news""Caltex pumps for control""Watchdog blocks Caltex bid""Exxon Mobil sells service station network""Mobil Oil New Zealand Limited is New Zealand's oldest oil company, with predecessor companies having first established a presence in the country in 1896""ExxonMobil subsidiaries have a business history in New Zealand stretching back more than 120 years. We are involved in petroleum refining and distribution and the marketing of fuels, lubricants and chemical products""Archived copy"the original"Exxon Mobil to Sell Its Japanese Arm for $3.9 Billion""Gas station merger will end Esso and Mobil's long run in Japan""Esso moves to affiliate itself with PC Optimum, no longer Aeroplan, in loyalty point switch""Mobil brand of gas stations to launch in Canada after deal for 213 Loblaws-owned locations""Mobil Nears Completion of Rebranding 200 Loblaw Gas Stations""Learn about ExxonMobil's operations in Egypt""Petrol and Diesel Service Stations in Egypt - Mobil"Official websiteExxon Mobil corporate websiteMobil Industrial official websiteeeeeeeeDA04275022275790-40000 0001 0860 5061n82045453134887257134887257

Frič See also Navigation menuinternal link

Identify plant with long narrow paired leaves and reddish stems Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?What is this plant with long sharp leaves? Is it a weed?What is this 3ft high, stalky plant, with mid sized narrow leaves?What is this young shrub with opposite ovate, crenate leaves and reddish stems?What is this plant with large broad serrated leaves?Identify this upright branching weed with long leaves and reddish stemsPlease help me identify this bulbous plant with long, broad leaves and white flowersWhat is this small annual with narrow gray/green leaves and rust colored daisy-type flowers?What is this chilli plant?Does anyone know what type of chilli plant this is?Help identify this plant