tags:

views:

128

answers:

4

I have created a simple bash script, chmod +x, and successfully am running it as a background service.

But, the script is called "sh" or "sleep" or whatever command seems to be running at the time, not my script name, when I view a process list.

How do I name the process of my bash script so I can distinguish it? I want to be sure that I'm not running the script more than once.

I am very new to bash scripting... sorry if this is a dumb question.

I am using #!/bin/bash

+1  A: 

Since that's what ps also shows I have a hunch you're out of luck. Sorry but shell scripts can't change their apparent process name.

However, for the cases that show bash you can create a symlink to bash under a name descriptive to your script and invoke your script via that symlink.

Joshua
argv[0] can actually be spoofed to whatever you want. Try it: char *args[] = {"Hello world", NULL}; execvp("sh", args); Of course the shell doesn't expose this...
asveikau
A: 

Not sure how portable my solution will be, but it works on Linux.

If you really want to do this (maybe you want to be able to kill your process by looking it up by name), you can write a small C program to call into the shell with a different process name.. For example...

#include <unistd.h>
#include <stdio.h>

int main(int argc, char **argv)
{
   if (argv[1])
   {
      argv[0] = argv[1];
      return execvp("sh", argv);
   }
   else
   {
      fprintf(stderr, "usage: %s <script> [args]\n", argv[0]);
      return 1;
   }
}

Say that's called wrapper.c. You can compile with:

gcc -o wrapper wrapper.c

Then you can run:

./wrapper ./my-script

And check top or ps. It should have a "forged" program name..

Now... Whether you actually want to do this? I don't know. It's probably not worth it. Most people don't bother with this sort of thing.

asveikau
+1  A: 

Your parent shell will be running the whole time. That will be sh. Any other processes spawned by that shell will also be running. Try pstree to show parent-child relationships.

BTW, if you use bash-specific features that aren't in POSIX Bourne shell, you should use #!/bin/bash, not #!/bin/sh. Some systems have bash, but have a lighter-weight /bin/sh.

I am very new to bash scripting... sorry if this is a dumb question.

Not dumb. Basic, but only once you understand how Unix processes work, (and how whatever you're using in OS X that shows you "service" names, since that's not a word that would make sense in any Unix context in this situation.) So you're dealing with a fair amount of complexity, and I don't blame you for asking.

Maybe OS X looks at process group leaders or something to come up with a "service name", if that's what it really calls it. I think that would be the process name of whatever process is running in the foreground (i.e. that you didn't fork off with & at the end of it, so the shell is waiting for it before executing the next command.)

Peter Cordes
+1  A: 

Do you have a "shebang" in your script?

I just did a little test. I found that with no shebang, the test script showed in ps as whatever command was executing at the time. However, if I, as I usually do, put:

#!/bin/bash

or

#!/bin/sh

(which on my system is symlinked to /bin/dash) as the first line in the script then the script showed up under its own name in the output of ps.

Dennis Williamson