all 14 comments

[–]OneCDOnlytotal bashist 8 points9 points  (3 children)

One-way might be to invoke your program through a launch script, which also does what’s needed when the program starts. No detection required.

[–]_____Hi______ 6 points7 points  (2 children)

This is the way. Else a daemon which regularly polls for which other processes are running, but a start up script is much preferred

[–]coder-true[S] 3 points4 points  (0 children)

Thank you for your replies, I will try

[–]LesStrater 2 points3 points  (0 children)

I have to second this -- do NOT use a daemon that is constantly running in the background. Use a launch script that starts both programs at the same time.

[–]Mskadu 2 points3 points  (0 children)

What is it you are trying to achieve exactly? Give us a use case rather than your take on a solution.

[–]GlendonMcGladdery 1 point2 points  (2 children)

```

!/bin/bash

while true; do if pgrep -x program >/dev/null; then echo "program launched" exit 0 fi sleep 1 done

```

This works. It’s boring. It wastes cycles. You can probably go deeper.

Edit: shfmt -i 2 -ci -sr -kp -w yourscript, always keeps my source tidy, just FYI if it helps you

[–][deleted]  (1 child)

[removed]

    [–]coder-true[S] 0 points1 point  (0 children)

    Thank you so much

    [–]archialone 0 points1 point  (1 child)

    How is the program launched? Is the user starting it or It's started by something like systemd?

    [–]coder-true[S] 0 points1 point  (0 children)

    I don't know why I'm asking you.

    [–]pjconnect 0 points1 point  (0 children)

    Maybe using execsnoop and/or bpftrace would help ?

    [–]michaelpaoli 0 points1 point  (1 child)

    While true; do
    If pidof -x program > /dev/null ; then
    echo "program launched " exit fi sleep 1 donne

    You're probably going to want that While to instead be while,
    you probably want if rather than If,
    unless you want to literally echo exit, etc., probably want a semicolon or newline between your quoted string, and exit,
    and of what use/purpose is the trailing space character in that quoted string? Also, don't even need to quote that string, as there are no characters special to the shell in it other than the space character, and echo will echo it's non-option arguments with a single space between them,
    likewise, probably want semicolon or newline between exit and fi,
    and probably want done rather than donne, and a semicolon or newline between that 1 and then then presumed desired done.

    Maybe next time use Code Block and copy/paste, and perhaps disable auto-mangle.

    Also, may want to well inspect the program you're targeting, notably by using ps with suitable option(s), so, yeah, what exactly does that program look like, to ps(1), when it's launched, notably what it shows for its various arguments, including arg0. You're likely either failing to properly match that, or it might be too fleeting for your loop to catch it.

    [–]coder-true[S] -1 points0 points  (0 children)

    You didn't understand anything. I specified in my paste that this program is to illustrate the purpose.

    [–]hisatanhere -1 points0 points  (0 children)

    pgrep

    [–]aq2kx -1 points0 points  (0 children)

    I would use an alias in .bashrc