Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need to set up the following script to run as root whenever I log in:

#!/bin/zsh
truncate -s 0 ~yourstruly/.juniper_networks/network_connect/ncsvc.log

I know how to run the script as root interactively (namely through sudo), but I can't figure out how to make the script run as root non-interactively whenever I log in.

How can I do this?

FWIW: the script is currently owned by root, has permissions 6755, and the file to be truncated is also owned by root and has permissions 0644, but when I run the script, the call to truncate fails with a "Permission denied" error.

share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

The permissions of the script are irrelevant. Setuid and setgid bits are ignored on scripts on Linux and most other unices.

You can configure sudo to allow you to run this script without specifying a password, with the NOPASSWD tag. Make sure that sudo doesn't require to be connected to a terminal: turn off the requiretty option. Note that the NOPASSWD-tagged rule must come before any other rule that would let you run the command with a password prompt, because the first match applies. So run visudo:

Defaults !requiretty
kjo ALL = NOPASSWD: /path/to/nc-logs-truncate
kjo ALL = (ALL) ALL

Change the first line of the script to #!/bin/zsh -f, otherwise zsh will load ~/.zshenv which isn't desirable here.

Now that I've answered your literal question, this is probably not the best method. A simple chown kjo ~/.juniper_networks/network_connect/ncsvc.log would ensure that the log file belongs to you so you can truncate it at will.

Better, move the log file instead of truncating it: it's often useful to keep recent logs around (for example to compare working logs to non-working logs if nc fails). Give yourself write permission on the directory (you might own it already) and

mv -f ~/.juniper_networks/network_connect/ncsvc.log ~/.juniper_networks/network_connect/ncsvc.log.0
touch ~/.juniper_networks/network_connect/ncsvc.log

Alternatively, make a more unixy NC package, tell ncsvc to write its logs in /var/log, and include its log in the logrotate configuration.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.