How to enable a system-wide function for users including sudo?

后端 未结 2 1983
独厮守ぢ
独厮守ぢ 2021-01-22 10:12

I want to make a global alias for killprocessatport so I put at the end in /etc/bash.bashrc

fuserfunction() {
    fuser -KILL -k -n tcp         


        
相关标签:
2条回答
  • 2021-01-22 10:36

    it does not work because when you do:

    bash /etc/profile
    

    you open a new shell, execute the profile script, and get back to your current shell.

    Actually, that's why to load the content of /etc/bash.bashrc you do source /etc/bash.bashrc in the /etc/profile.

    Thus to load it in your current shell, you should run:

    source /etc/profile
    

    instead.

    Nota Bene:

    1. It's though a better idea to add your own aliases in ~/.bashrc which gets automatically sourced by bash on load ;
    2. If you really want it to be global, follow @user1257931's suggestion about putting it in /etc/profile.d where it will be automatically sourced on new shell instances ;
    3. As @wich is suggesting, there's no reason to add an alias for killprocessatport to fuserfunction. Though, you may prefer to do an alias for a call to the function with a parameter like: alias killhttpserver=fuserfunction 80
    4. It'd be also a good idea to use an explicit name for your fuserfunction, something like fuserkillproc or even something better you may come up with…

    HTH

    0 讨论(0)
  • 2021-01-22 10:45

    You can't use an alias or a function in this case. Create an executable script in a location that's on your PATH (as configured by sudo, if your /etc/suoders modifies root's PATH).

    #!/bin/sh
    exec fuser -KILL -k -n tcp "$@"
    

    Save the script, then set its permissions with chmod +x.

    0 讨论(0)
提交回复
热议问题