Run script with arguments as sudo without being asked for password
I have two programs in Java that I'm calling from bash script:
java program1 arg ... arg
sudo java program2 arg ... arg
I want to run program2 as sudo without being asked for password. I'm aware of sudoers file, but I'm not sure how to configure it to run java programs as sudo without being asked for password. Any thoughts?
Using wildcards in the sudoers file
In the sudoers
file, you can use wildcards (*), thus creating the possibility to run a script with arguments.
An example
I tested it with a shockingly simple python script. In the sudoers
file I had to use the absolute path to python
: /usr/bin/python
in the line of the sudoers file:
jacob ALL=NOPASSWD: /usr/bin/python /home/jacob/Bureaublad/pscript_1.py*
Then my (python script) code was:
#!/usr/bin/python
import sys
s1 = sys.argv[1]
s2 = sys.argv[2]
print(s1)
print(s2)
Then I ran in a terminal:
~$ sudo python '/home/jacob/Bureaublad/pscript_1.py' monkey banana
monkey
banana
without asking for my password.
Similarly, you should be able to achieve what you want by adding the following line to the sudoers
file:
<your_username> ALL=NOPASSWD: /path/to/java <your_script>*
And run the script by:
sudo java <script> <args>
and you will not be asked for your password.
More information
An interesting source on how to use the sudoers
file might be this one.