问题
Case :
My case is pretty straight forward. I have a Linux server machine. On that server, there are 2 users. One is root and the other is oracle. Now here is the thing, the root user does not know the sqlldr command, this command is recognized by oracle user only.
I have a batch.sh file and in that file i have an sqlldr command that will insert record in the database. When i open terminal using oracle user, that batch.sh file runs according to the expectations. But when I run that batch file from terminal using the root user, it says "./batch.sh: line 3: sqlldr: command not found".
Problem:
Now the problem is, I am calling this batch.sh file from java code, and this java process is started as root. There is no way for me to switch this java process to oracle. However what I am expecting is, that i may switch user before executing the command in the batch.sh file using the root user.
Here is my batch.sh file content.
#!/bin/bash
sqlldr username/password@sid control='/path/toFile/control.txt' log='/path/toFile/Results.log' bad='/path/toFile/BadFile.bad' ERRORS=5000
exit 0
I am expecting something like this.
#!/bin/bash
### Start - Switch user here from root to oracle so that it recognize the following command
sqlldr username/password@sid control='/path/toFile/control.txt' log='/path/toFile/Results.log' bad='/path/toFile/BadFile.bad' ERRORS=5000
### End - Undo the switch that we made above
exit 0
Any help will be highly appreciated.
Thanks
回答1:
You could also have root run the command as that user in your script with runuser:
runuser -l oracle -c "sqlldr username/password@sid control='/path/toFile/control.txt' log='/path/toFile/Results.log' bad='/path/toFile/BadFile.bad' ERRORS=5000"
回答2:
The executable sqlldr is simply not in the root's search PATH. So, when you are logged in as root, you will not be able to execute sqlldr.
What you need to do is find out where sqlldr is located (eg. /folderof/yourtool) and do A or B:
A) make sure you include it in the root path that is loaded when root logs in, using the method according to your linux distribution
or
B) add the line PATH=$PATH:/folderof/yourtool
to the shell script or java environment to dynamically add the PATH.
来源:https://stackoverflow.com/questions/40676310/run-as-other-user-option-in-linux