start-process with a special name, or handle to kill it
One method is to use the -PassThru
parameter, which causes Start-Process
to return an object which can be used to control the process. When you're done with the process, pipe the object to Stop-Process
(or call the object's Kill()
method)
In cases where you need to store the object across PS sessions, you can save the variable to an XML file using Export-Clixml
. Later, rehydrate the variable using Import-Clixml
.
$proc = Start-Process notepad -Passthru
$proc | Export-Clixml -Path (Join-Path $ENV:temp 'processhandle.xml')
PS Session #2$proc = Import-Clixml -Path (Join-Path $ENV:temp 'processhandle.xml')
$proc | Stop-Process
How to set the process name of a shell script?
Here's a way to do it, it is a hack/workaround but it works pretty good. Feel free to tweak it to your needs, it certainly needs some checks on the symbolic link creation or using a tmp folder to avoid possible race conditions (if they are problematic in your case).
Demonstration
wrapper
#!/bin/bash
script="./dummy"
newname="./killme"
rm -iv "$newname"
ln -s "$script" "$newname"
exec "$newname" "$@"
dummy
#!/bin/bash
echo "I am $0"
echo "my params: $@"
ps aux | grep bash
echo "sleeping 10s... Kill me!"
sleep 10
Test it using:
chmod +x dummy wrapper
./wrapper some params
In another terminal, kill it using:
killall killme
Notes
Make sure you can write in your current folder (current working directory).
If your current command is:
/path/to/file -q --params somefile1 somefile2
Set the script variable in wrapper to /path/to/file (instead of ./dummy) and call wrapper like this:
./wrapper -q --params somefile1 somefile2
Set a process name in C#
Extending what Alexei said - you could create a Dictionary< int, string > to keep track of the process ID / descriptive name that you create. Maybe you should write this out to a file in case your program crashes - but you'd need some special startup handling to deal with processes exiting.
On startup you'd want to read in the file, and check current processes to see if they match with what you have, and remove any processes that no longer exist (wrong process id or exe name). You might want to do that every time you create a new process and write to the file.
How to give the java process a name in the operating system (other than java)
If you need to be able to differentiate between different java programs you can use the jps command that gives you a list of all java processes and running your program with
java -Dname=myFirstService -cp myFirstService.jar some.client.main.MyFirstService
then if you do a:
jps -v
You will see your process correctly.
If you need to change the process name at the OS level I recommend you use http://launch4j.sourceforge.net/
python multiprocessing - access the process name inside the function called with Process.start(target=func)
You can use the current_process
function:
from multiprocessing import Process, current_process
def somefunc():
print current_process().name
if __name__ == '__main__':
p = Process(target=somefunc)
p.start()
print p.name
How to start a PHP process with a name?
Check what the top level PHP script is using ps axuf
or a similar command.
PowerShell - How to use 'Start-Process' and rename the newly launched windowtitle
A "dirty" solution would be:
start-process powershell.exe -argument "`$host.ui.RawUI.WindowTitle = 'New window title rename example text'; get-content -Path $c -wait"
I would recommend creating a script for you commands and use parameters for input.
Untitled2.ps1
param($c)
$host.ui.RawUI.WindowTitle = 'New window title rename example text'
Get-Content -Path $c -Wait
Script
$script = Get-Item ".\Desktop\Untitled2.ps1"
$c = Get-Item ".\Desktop\t.txt"
Start-Process powershell.exe -ArgumentList "-File $($script.FullName) -c $($c.FullName)"
Is there a way to change effective process name in Python?
Simply put, there's no portable way. You'll have to test for the system and use the preferred method for that system.
Further, I'm confused about what you mean by process names on Windows.
Do you mean a service name? I presume so, because nothing else really makes any sense (at least to my non-Windows using brain).
If so, you need to use Tim Golden's WMI interface and call the .Change method on the service... at least according to his tutorial.
For Linux none of the methods I found worked except for this poorly packaged module that sets argv[0] for you.
I don't even know if this will work on BSD variants (which does have a setproctitle system call). I'm pretty sure argv[0] won't work on Solaris.
Related Topics
Find the Process Run by Nohup Command
Mmap: Will the Mapped File Be Loaded into Memory Immediately
What Is File Hole and How Can It Be Used
Difference Between .Dynamic .Dynsym and .Dynstr in an Elf Executable
How to Grep Exact Literal String (No Regex)
Using the Universal Chess Interface
Dma Cache Coherence Management
How to Pipe Output to a File When Running as a Systemd Service
Find Directories Having Size Greater Than X Mb
How to Diff Top Lines of Two Files Without Intermediate File
Pause Programmatically Video Player Mpv
Running R Scripts or Commands with Interpretor in Unix for Unix-Layman
Bash: Get List of Commands Starting with a Given String
200,000 Images in Single Folder in Linux, Performance Issue or Not
The Address Where Filename Has Been Loaded Is Missing [Gdb]
Convert a Time Span in Seconds to Formatted Time in Shell
Bumping Version Numbers for New Releases in Associated Files (Documentation)