Reputation: 2244
I would like to provide individual names to the threads in my Rust program. These names should appear in top
/htop
so I can differentiate the thread's job. In Ruby I would modify the argv[0]
entry, or maybe Process.setproctitle("foo")
. My inspiration is from Ruby's Unicorn web server.
The env::args
function returns an Args
value. The Args
value wraps the platform-specific std::sys::args::args()
function which is not reexported for general use. ArgsOs
doesn't have a usable mutator (so yes, the stdlib makes it clear it is immutable).
How do I mutate the arguments some other way? I am happy with a Linux-only solution since that is where the real work will happen. Solutions outside the stdlib are also fine.
What path should I take in order to modify the first argument in the environment of my process?
Upvotes: 3
Views: 1835
Reputation: 124
I wrote the proctitle crate for setting process titles in a cross-platform manner. On Linux it does happen to name the current thread, but this is a quirk of the APIs it provides rather than a deliberate choice.
Upvotes: 2
Reputation: 430673
How can I replace an entry in
std::env:args()
You cannot. Immutable means immutable.
I would like to change how my Rust program appears in
top
/htop
There is nothing like this in the standard library. As far as I know, there's no cross-platform solution, so it would be a hard fight to put in there.
Seems to have a simple enough solution: Change process name without changing argv[0] in Linux
If you only care about Linux, you can use the prctl crate:
prctl::set_name("new_process")
Has various concepts of "process name" and the solution is complex and uses undocumented / hidden APIs: Setting process name on Mac OS X at runtime
Everything in that answer could be written in Rust using the appropriate bindings to the macOS APIs.
Does not seem to have such a concept: Changing a process name in runtime
Someone could write a crate that abstracts across these different mechanisms, but I'm not aware of one.
so I can easily spot what all my threads are doing
As mentioned in the comments, when you create a thread, you can give it a name. Some work was recently put into renaming threads at run time, but that work stalled out.
Upvotes: 15