applescript quit application by process id - applescript

Is it possible to quit an application in Applescript using process id(unix id in AS)? I have multiple instances of same application running and need to close one specific instance.
Something like:
tell application App (whose process unix id is 14241) to quit

You can use the shell, for example:
do shell script "kill 14241"

Related

Make golang program restart itself

Im writing a tool and one of its commands allows you to start a new session
How can I make a golang program restart itself? If your solution is OS-Strict im on Linux.
I tried
// exec from os/exec
exec.Command(os.Args[0]).Run()
but it doesnt work. I get a blank input session which is hard to explain
My Program Input: session new
:(
:(
each of the :( represent a blank line where im able to type stuff and hit enter, there are 2 which means i hit enter twice
Im expecting
My Program Input: session new
My Program Input:
Edit: more accurately, i want to make a subprocess of the same program
You could use a separate process, like radovskyb/gobeat.
Example:
sudo gobeat -pid=1234 -cmd="go run sendemail.go"
Run with sudo so gobeat will restart the server in the same terminal tty that it originated in. (sudo)
Point gobeat to the process of the running server that you want gobeat to monitor. (gobeat -pid=1234)
Set the cmd flag to run a Go file that will send an email notifying you that the server was restarted. (-cmd="go run sendemail.go")
If you do not want a separate process, then consider implementing a graceful upgrade
You can use the library cloudflare/tableflip for instance.

Start application on windows startup / boot but allow it visible to users when logged in?

I've tried using Scheduled Tasks using "Run whether user is logged on or not", but it appears to run the application in a separate instance even if its started when the user is logged in already.
Is there a way to do this programatically? Does windows allow this at all or will I need to stop the program(s) and restart them under the user credentials?
Your problem is that the tasks are launched as background tasks so the user cannot access them.
You should launch cmd.exe via "the Sheduled Tasks" and launch your application through cmd (passing it by parameters).
see : How to launch cmd.exe to execute command using Task Scheduler ?

How to know some file is currently executing or not?

I'm using Ubuntu. I have two bash script files. Both will run in parallel. Now I want to continuously monitor on another file that it is running or not.
So any way to find that the file is currently executing or not ?
Numerous possibilities, it is a question of creativity...
Some suggestions:
periodically poll the process list and filter it by name or process id
start the script with control sockets, as long as the sockets are open the script runs
use the usual locking strategy in file system.
have the script do a lifebeat on a regular base, then watch that lifebeat.
start the script in a series of commands, the moment the script exists the next command will be executed by the calling shell. That one could be a notification script or something.
have the script do some wiggling on your desktop and watch it yourself.
start it using nohup and watch the log file.
implement a deamon inside the script and connect periodically.
open a file from within the script and watch the file system using the fuser system call.
periodically write a token into a file by the monitoring script and have the monitored script remove that token, like a baton.
call the script using a blocking call. The script executes as long as that blocking call does not return.
create a singleton strategy on process level and simply try starting it periodically.
make the monitoring script act as a monitor deamon the executing script connects to. If the connection is terminated the scipt obviously has stopped executing.
...
Sorry, this starts getting boring...

NSRunningApplication can't recognize PID of Window Server?

In my Cocoa app I am detecting visible windows and getting PID of their owners. Then for ever PID I create NSRunningApplication object (using runningApplicationWithProcessIdentifier:) to get additional info (localized name, path, attributes etc.)
Everything works fine until I try to create NSRunningApplication with PID 88 which belongs (in my case) to Window Server. It just do not create that object and in accordance with Apple manual it returns nil - in other words: There is no process with that PID.
How is it possible? If it wouldn't exist I would't be able to get its PID and ps would't show it.
Any advice?
From the documentation (emphasis added):
Overview
NSRunningApplication is a class to manipulate and provide information for a single instance of an application. Only user applications are tracked; this does not provide information about every process on the system.
The window server is not a user process. If it does not work with admin privileges then you won't be able to do so at all.

How do I keep track of related windows in X11?

Unfortunately, my question is not as simple as keeping track of two windows created by the same process.
Here is what I have:
Two users, Jack and Jim are remotely logged in to the same Unix system and run X servers
Jack runs an application, 'AwesomeApp', that opens a GUI in a X window
Jim runs another instance of this application, opening his own GUI window
Now, Jack runs a supervisor application that will communicate with the process owning the first window (eg 'AwesomeApp') because it's HIS instance of 'AwesomeApp'
How can his instance of the supervisor find which instance of 'AwesomeApp' window is his own?
Aaaahhhh...looking it up on a per-user basis yes that could work.
As long as I tell the users that they cannot log in with the same user account from two different places.
You can use pgrep to get the process ID of Jack's instance of AwesomeApp:
pgrep -u Jack AwesomeApp
So if you launch the supervisor application from a shell script, you could do something like the following:
AWESOME_ID=`pgrep -u $USER AwesomeApp 2>/dev/null`
# run the supervisor application and pass the process id as the argument
supervisor $AWESOME_ID
Alternatively, if you don't want to use external programs like pgrep or ps, you could always try looking for the process in /proc directly.

Resources