Frage

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 $1
}
alias killprocessatport=fuserfunction

Then i put in /etc/profile at the very end

source /etc/bash.bashrc

And then I load it with bash /etc/profileexpecting the alias with parameter to work but it doesn't:

$ killprocessatport 80
killprocessatport: command not found

Why not?

War es hilfreich?

Lösung 2

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.

Andere Tipps

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top