Skip to content
This repository has been archived by the owner on Sep 23, 2021. It is now read-only.

Executing a sudo command like "sudo -v" #44

Closed
NickHatBoecker opened this issue Mar 13, 2017 · 3 comments
Closed

Executing a sudo command like "sudo -v" #44

NickHatBoecker opened this issue Mar 13, 2017 · 3 comments

Comments

@NickHatBoecker
Copy link

Hey there,

thanks for this awesome package!

I have a homebrew script to switch between php versions on my macbook.
You can't execute the script with sudo, so the normal command would be phpswitch.sh 70 in order to switch to php 7.0.

But at the end of the script the apache config is updated and the apache will be restarted. So the script needs root rights.

I search for some workaround and I found the possibility to call sudo -v first, so the root password is prompted and then executing the phpswitch script, because then the timestamp for sudo is already valid.

Now the problem with this workaround: with your package I cant call sudo.exec('sudo -v') nor sudo.exec('-v').

Do you have any idea how I could achieve this?

Thanks in advance!

@jorangreef
Copy link
Owner

Thanks @theskyliner !

If I understand correctly, do you want to do the following?

# Get the sudo session established for the next few minutes:
sudo -v
# Run the following script without sudo for the most part.
# Within this script there is a command prefixed with sudo to restart apache.
./phpswitch.sh 70

@NickHatBoecker
Copy link
Author

Yup, thats exactly what I meant

@jorangreef jorangreef reopened this Mar 29, 2017
@jorangreef
Copy link
Owner

sudo-prompt already does an equivalent of sudo -v when it runs, to try and keep the user's sudo session alive for the next time exec() is called. However, TTY tickets are now enabled by default from macOS Sierra on, so this does not help since each exec() call is run in a new TTY.

I would recommend extracting the sudo command from your script and running it using sudo-prompt, e.g.:

./phpswitch.sh 70
sudo.exec('restart apache')

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants