Command behavior is different when executed by an other user (via su -c)

Quentin Morrier

I want to kill every processes who match a specific chain. Here is my script, which works pretty well:

echo `ps aux | grep verySpecificChain | grep -v grep | /usr/bin/awk '{ print $2 }'` | xargs kill

Now I want to execute this script from an other user via `su -c:

echo password | su -c "echo `ps aux | grep verySpecificChain | grep -v grep | /usr/bin/awk '{ print $2 }'` | xargs kill" userName;

My problem is that when grep verySpecificChain matches several processes, only the first element is passed to xargs kill:

30598  ==> killed
bash: line 1: 30599: command not found  ==> Not killed
bash: line 2: 30600: command not found  ==> Not killed
bash: line 3: 30606: command not found  ==> Not killed

I really would like to understand why with or without su -c the command behaviour changes?

I'm running GNU bash, version 4.2.53(1)-release (x86_64-redhat-linux-gnu) on Fedora 20.

Kamil Maciorowski

I cannot explain what exactly fails in your example (so I admit it is some kind of voodoo programming on my side), but this is a fix that (almost -- see below*) works in my bash (Debian):

  • get rid of inside echo;
  • escape $2.

Result:

echo password | su -c "ps aux | grep verySpecificChain | grep -v grep | /usr/bin/awk '{ print \$2 }' | xargs kill" userName;

*I wrote "almost works" because Debian won't let me use su in a pipe to echo the password. I must run it without initial echo and type the password interactively. I guess it's not an issue in OP's Fedora.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Run .zshrc when passing command via -c

분류에서Dev

limit user to only a single command: "su -"

분류에서Dev

How to run command as different user

분류에서Dev

su <USER> -c "command"에 여러 매개 변수를 전달하는 방법

분류에서Dev

Permission denied when writing to /tmp directory when using su command

분류에서Dev

CentOS 7: Can't connect via WinSCP when bash is executed

분류에서Dev

Which command is executed when selecting an entry in Unity dock?

분류에서Dev

When in debian 8.1 why is the super user run with su not sudo?

분류에서Dev

What does it mean when code is executed in [kernel|user] mode?

분류에서Dev

How is a bash script executed via its filename as command name with and without shebang?

분류에서Dev

user1 및 su-user1 -c "command"로 실행할 때 다른 결과가 나타나는 이유는 무엇입니까?

분류에서Dev

Different behavior when launched from terminal instead of Unity launcher

분류에서Dev

Ansible : "sudo su -c <COMMAND> <USER>"를 사용하여 다른 사용자로 명령을 실행하려면 어떻게해야합니까?

분류에서Dev

What is the actual command executed when running a script with a shebang as its name with same arguments?

분류에서Dev

Amazon EC2 password prompt when "su - ec2-user"

분류에서Dev

Detect use of su command in bash

분류에서Dev

terminal appears on hotkey but command not executed

분류에서Dev

Replace string in command to be executed in zsh

분류에서Dev

nmap command - suspicious behavior

분류에서Dev

Multiple objects, different behavior

분류에서Dev

Access to table via Stored Procedure when the table is in a different schema

분류에서Dev

SSIS Package for WinSCP fails with error code 3 but works via command prompt and on other servers

분류에서Dev

How can I setup a callback to be executed when function returns in c++

분류에서Dev

A strange behavior for globbing in find command

분류에서Dev

Sudo command not found (su login not working)

분류에서Dev

Debain(buster) su not working properly, command not found

분류에서Dev

Return last command executed in shell-script

분류에서Dev

bash: propagating exit code of command executed in alias

분류에서Dev

Different compilers behavior for template specialization

Related 관련 기사

  1. 1

    Run .zshrc when passing command via -c

  2. 2

    limit user to only a single command: "su -"

  3. 3

    How to run command as different user

  4. 4

    su <USER> -c "command"에 여러 매개 변수를 전달하는 방법

  5. 5

    Permission denied when writing to /tmp directory when using su command

  6. 6

    CentOS 7: Can't connect via WinSCP when bash is executed

  7. 7

    Which command is executed when selecting an entry in Unity dock?

  8. 8

    When in debian 8.1 why is the super user run with su not sudo?

  9. 9

    What does it mean when code is executed in [kernel|user] mode?

  10. 10

    How is a bash script executed via its filename as command name with and without shebang?

  11. 11

    user1 및 su-user1 -c "command"로 실행할 때 다른 결과가 나타나는 이유는 무엇입니까?

  12. 12

    Different behavior when launched from terminal instead of Unity launcher

  13. 13

    Ansible : "sudo su -c <COMMAND> <USER>"를 사용하여 다른 사용자로 명령을 실행하려면 어떻게해야합니까?

  14. 14

    What is the actual command executed when running a script with a shebang as its name with same arguments?

  15. 15

    Amazon EC2 password prompt when "su - ec2-user"

  16. 16

    Detect use of su command in bash

  17. 17

    terminal appears on hotkey but command not executed

  18. 18

    Replace string in command to be executed in zsh

  19. 19

    nmap command - suspicious behavior

  20. 20

    Multiple objects, different behavior

  21. 21

    Access to table via Stored Procedure when the table is in a different schema

  22. 22

    SSIS Package for WinSCP fails with error code 3 but works via command prompt and on other servers

  23. 23

    How can I setup a callback to be executed when function returns in c++

  24. 24

    A strange behavior for globbing in find command

  25. 25

    Sudo command not found (su login not working)

  26. 26

    Debain(buster) su not working properly, command not found

  27. 27

    Return last command executed in shell-script

  28. 28

    bash: propagating exit code of command executed in alias

  29. 29

    Different compilers behavior for template specialization

뜨겁다태그

보관