I´m writing a Powershell script to do a bunch of things and when finished it will be run as a scheduled task. For that reason I want to be able to check whether an older instance is still alive when I start running the script and kill the older one if it exists.
I was thinking I would use something like this:
$process = Get_Process | $name
$process.kill
But how to get the $name variable in a simple way?
Does anyone have a better suggestion?
Best regards,
Gísli
You can do this in windows scheduled task configuration. The settings depends on the OS you are using though.
EDIT: that is you can configure the task to be killed after a certain period of time (i.e. when your next one starts).
Why do you need to get the name? Get-Process returns high fidelity Process objects and you can operate on it directly.
To get a process of a particular name use $n = Get-Process notepad, say, and then do $n.kill() to kill it. If you do need to check the name again, do $n.Name. To see what properties and methods you can use, try $n | get-member
And make sure you read the manual: http://technet.microsoft.com/en-us/library/dd347630.aspx
Related
My program checks if there is a new version of itself. If yes it would exit and start an updater that replaces it and then restarts.
My problem is that I haven't found any info on how to make process start right after closing the actual program.
Any suggestions?
Thanks in advance
I intended to add a comment, but I'm too low in points here. The updater itself should probably contain a check to determine whether your application is running an instance, and it should contain a timeout loop that performs this check and factor the timeout following it's startup state. That way you can awaken it, and close your application. The updater should just determine your application is not running, compare versions perform the intended update operation.
a possible solution would also be to create a task via tash sceduler or cron job, starting an out of process application, like CMD.exe.. which brings me to my original comment-question: in regards to what Operating System(s) and Platform(s) is your program intended for?
I'm writing a command line application in Mac using Objective-c
At the start of the application, i want to check if another instance of the same application is already running. If it is, then i should be either wait for it to finish or exit the current instance or quit the other instance etc.
Is there any way of doing this?
The standard Unix solution for this is to create a "run file". When you start up, you try to create that file and write your pid to it if it doesn't exist; if it does exist, read the pid out of it, and if there's a running program with that pid and your process name, wait/exit/whatever.
The question is, where do you put that file, and what do you call it?
Well, first, you have to decide what exactly "already running" means. Obviously not "anywhere in the world", but it could be anything from "anywhere on the current machine" to "in the current desktop session". (For example, if User A starts your program, pauses it, then User B comes along and takes over the computer via Fast User Switching, should she be able to run the program, or not?)
For pretty much any reasonable answer to that question, there's an obvious pathname pattern. For example, on a Mac, /tmp is shared system-wide, while $TMPDIR is specific to a given session, so, e.g., /tmp/${ARGV[0]}.pid is a good way to say "only one copy on the machine, period", while ${TMPDIR}/${ARGV[0]}.pid is a good way to say "only one copy per session".
Simple but common way to do this is to check the process list for the name of your executable.
ps - A | grep <your executable name>
Thank you #abarnert.
This is how I have presently implemented. At the start of the main(), I would check if a file named .lock exists in the binary's own directory (I am considering moving it to /tmp). If it is, application exits.
If not, it would create the file.
At the end of the application, the .lock file is removed
I haven't yet written the pid to that file, but I will when exiting the previous instance is required (as of yet I don't need it, but may in the future).
I think PID can be retrieved using
int myPID=[[NSProcessInfo processInfo] processIdentifier];
The program will be invoked by a custom scheduler which is running as a root daemon. So it would be run as root.
Seeing the answers, I would assume that there is no direct method of solving the problem.
We use PHP gearman workers to run various tasks in parallel. Everything works just fine, and I have silly little shell script to spin them up when I want them. Being a programmer (and therefore lazy), I wanted to see if I could spin these up via an upstart script.
I figured out how to use the instance stanza, so I could start them with an instance number:
description "Async insert workers"
author "Mike Grunder"
env SCRIPT_PATH="/path/to/my/script"
instance $N
script
php $SCRIPT_PATH/worker.php
end script
And this works great, to start them like so:
sudo start async-worker N=1
sudo start async-worker N=2
The way I want to use these workers is to spin up some number of them (maybe one per core, etc), and I would like to do this on startup. To be clear, I don't need the upstart script to detect the number of cores. I'm happy to just say "do 8 instances", but that's why I want multiple running. Is there a way for me to use the "start on" clause in an upstart script to do this automatically?
For example, start instance 1, 2, 3, 4? Then have them exit on shutdown properly?
I suppose I could hook this into an init.d script, but I was wondering if upstart can handle something like this, or if anyone has figured out this issue.
Cheers guys!
What you need is a bootstrap task that runs on startup and iterates over all your worker jobs, starting each one.
#/etc/init/async-workers-all.conf
start on runlevel [2345]
task
env NUM_WORKERS=8
script
for i in `seq 1 $NUM_WORKERS`
do
start async-worker N=$i
done
end script
The key is to make this a task, which tells upstart to let the task run to completion before emitting any events for it. See http://upstart.ubuntu.com/cookbook/#task and http://upstart.ubuntu.com/cookbook/#instance
I've found this question, but even when is close to what I need, is useless
Basically, I have an app that needs to do something when another process (of known name) is launched and/or terminated, but i don't have the PID, so i can't set a kqueue to look for it.
I could do a while for "ps aux | grep processtolook | grep -v grep" command, but that's my last resort.
Any ideas?
Look at this answer: Determine Process Info Programmatically in Darwin/OSX. The libproc.h header file has proc_listpids which will get you all the pids. You can then get the pid information in a loop and using proc_pidinfo and check the name. Looking at the top source as suggested there might also be worthwhile. (The current verson is here http://www.opensource.apple.com/source/top/top-67/.)
Unfortunately, this is an undocumented interface and subject to change at any time. Also, it isn't the quickest thing, since you have to loop over all the processes in the system. In general, this isn't a great way to do this.
A better way might be to write a new processtolook that simply invoked the old one which you can move or rename. It could then notify you of the process start and stop. Is that a possibility?
If the target process/program name which you want the PID is "processtolook" then you can use pidof command to get the PID of that running program.
pidof processtolook
have a look at the pidof manual. This is a part of sysvinit-tools package.
EDIT1:
Have a look at this code i found: http://programming-in-linux.blogspot.com/2008/03/get-process-id-by-name-in-c.html
This might help you get you the outline.
On BSD you can use kvm_openfiles and kvm_getprocs to implement what you want. I don't know if it's available on OSX. If you have a pgrep program, look how it's implemented (src/bin/pkill.c).
If I launched a shell script using AuthorizationExecuteWithPrivileges what would be the easiest way to kill the script and any other processes that it spawned.
Thanks
It's running as root, so you can't kill it from a regular-user process. You're going to have to ask it nicely to exit on its own.
Apple has sample code that uses stdout to pass the PID back to the caller.
Use the communications pipe that AuthorizationExecuteWithPrivileges() returns by reference in its last argument, FILE **communicationPipe, to send a message to the child process that asks it to take itself and its descendants out. It can then kill itself and all its descendants using kill(0, SIGINT), or, if more drastic measures are required, SIGKILL.
The message you use can be as simple as closing the file while the child waits for the file to close; at that point, it knows you're done talking to it and it's time to take itself out.
There are some caveats about the descendants that will actually receive this message, for which see the kill(2) manpage. The caveats mostly won't matter so long as the process you started via AEWP hasn't dropped privileges, though one implicit issue is that this approach won't work if any child processes have put themselves in a new process group.