Ask permission questions

If my script doesn’t add sudo
cat: /var/log/kern.log: Permission denied
If my script adds sudo
sudo: no tty present and no askpass program specified

Is there any good way

Perhaps use a non interactive sudo :

-n, --non-interactive
Avoid prompting the user for input of any kind. If a password is required for the command to run,
sudo will display an error message and exit.

1 Like

icinga2 Is the user executing the script nagois?

sudo -n
prompt:
sudo: a password is required

That depends on the system which executes the script.

e.g:
Ubuntu: nagios
CentOS: icinga
(possibly generalized: debian-based: nagios, redhat-based: icinga)

You can try to execute the script on the target/executing system, e.g.:

sudo -u nagios /path/to/script
sudo -u icinga /path/to/script

Additional note:
Both users do not have a login-shell.

Or you simply look whether the user nagios or icinga exist on the target system.

You need to add the sudo-rights for the specific user

I tested with sudo permissions added

nagios ALL=(ALL) NOPASSWD:ALL # That does not work

nagios ALL = NOPASSWD:ALL # this is ok