Changing process name in Mono

Banshee 0.10.5 supports changing the process name from “mono” to “banshee” using the prctl call. This makes “killall banshee” work. While this is nothing to marvel over, I have been a little taken aback by the amount of inquiry over this feature. In the past few days, a large number of people have asked me, “how did you do this.” Here’s a brief description of “how.”

This had been discussed over private email a few months ago, but was recently touched on again as a bug filed against Beagle.

The short answer is, if you want your mono app to be “killall-able,” then do this:

using System.Runtime.InteropServices;
using System.Text;


[DllImport ("libc")] // Linux
private static extern int prctl (int option, byte [] arg2, IntPtr arg3, 
    IntPtr arg4, IntPtr arg5);

[DllImport ("libc")] // BSD
private static extern void setproctitle (byte [] fmt, byte [] str_arg);

public static void SetProcessName (string name)
    try {
        if (prctl (15 /* PR_SET_NAME */, Encoding.ASCII.GetBytes (name + "\0"), 
            IntPtr.Zero, IntPtr.Zero, IntPtr.Zero) != 0) {
            throw new ApplicationException ("Error setting process name: " + 
                Mono.Unix.Native.Stdlib.GetLastError ());
    } catch (EntryPointNotFoundException) {
        setproctitle (Encoding.ASCII.GetBytes ("%s\0"), 
            Encoding.ASCII.GetBytes (name + "\0"));


try {
    SetProcessName ("banshee");
} catch (Exception e) {
    Console.Error.WriteLine ("Failed to set process name: {0}", e.Message);

Now “killall banshee” works. Also consider using “exec -a banshee ...” in your Bash wrapper to make process listings look better. There are more details provided in the bug, as well as some implications you should be aware of if you do this with your Mono app.

Code snippet updated on 2007-11-12 to show how to support BSD systems and reflect a known fix for 64 bit systems. The above code is known to be bug free and tested in many production applications.

14 Replies to “Changing process name in Mono”

  1. Awesome, I had a python module that would change sys.argv, but that only modifies /proc//cmdline, while killall looks at /proc//stat. This will allow me to stop having to use pkill -f, and go back to using killall :).

    This feature was only added in September ’04, and is a linux kernel feature. I think that’s an important thing to mention, because boxes running kernels without this feature may fall over on your media player, if you don’t catch and handle that exception nicely. :)

  2. Right, I should have mentioned prctl is Linux-only. Other systems support similar calls however, and when we get this in Mono itself, it will account for that.

    As for Banshee… yes, of course I handle a possible exception. However, Banshee is Linux only as well right now, mainly because of HAL. Banshee relies on other features that are mostly available on modern Linux systems, so chances are good you have a 2.6 kernel that post-dates Sept ’04.

  3. Could this be done with only managed code? If not, how could you do it multiplatform way? I suppose using #ifdef, am I right?

  4. No, you have to call something low-level, and that something is system-specific. Ideally the low-level, system-specific calls will be implemented in the Mono runtime in C, with a managed layer on top.

  5. Ideally the low-level, system-specific calls will be implemented in the Mono runtime in C, with a managed layer on top.

    If there is already a bug in bugzilla about this particular feature request or you open a new one, post the URL here please.

  6. BTW: wouldn’t it be nice to patch Mono so as to behave like this: by default, use this system call to put the name of the EXE file that holds the program that is running.

  7. It is pretty much _never_ correct to use ulong or long in a PInvoke signature. I know you are just passing zeros, but on a 32 bit machine you are overflowing the expected parameter passing space for the method by 12 bytes. YMMV.

    The conventional way to pass a ulong parameter to the native side is to use an IntPtr which will vary in size the same way a long does on the native side.

  8. You can do the process rename much simpler with a shell script (which is neded anyhow for user friendly invokation):

    exec -a myname mono foo.exe
    echo done.

    the script is not really portable which is not important since on windows you don’t need this rename hack anyhow.

  9. Mike: ah, yes. I remember doing that [improperly] for some signature a while ago. Thanks ;)

    Mirco: that’s what I mean by a combination of ‘exec -a’ and prctl. ‘exec -a’ does the argv[0] rename, which top, ps, gnome system monitor, and others will pick up and show in listings, while prctl actually renames the process, which killall and others will pick up. You have to do both to get the best coverage.

Comments are closed.