Does a su carry through to a script called by another script
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
My interpretation is that the second script, since it is invoked from within the first script, will inherit the user and environment from the first script.
To validate this, you can always echo out to a log file the user and environment information relevant to this and settle the issue.
Determine environment variables you wish to see and put in lines like this. Note if you use single >, you will create a new file, double >> appends, but will create if no file exists. Be careful in that it will place the file in the directory the script is running in. I usually use an explicit path when I do this.
In a login shell, type "env" and you'll see a dump of your environment and all the variable names so you can pick the ones that will be useful to you. PWD is also a good one.
Another good shell thing to do is to check returns from commands. For instance if you perform a copy, or move, or tar, something like that, you can check the return from that command:
Code:
cp file-a.txt file-b.txt;
echo $?
A ZERO indicates the copy worked. A non-ZERO indicates a fault. The "$?" is the shell variable for the default return from the last call.
When the AnotherScript.sh runs it doesn't work right when running in cron's env/shell, but it runs right if I run it like that, so i'm hoping if I run MyScript like that which runs AnotherScript, it will run properly.
When the AnotherScript.sh runs it doesn't work right when running in cron's env/shell, but it runs right if I run it like that, so i'm hoping if I run MyScript like that which runs AnotherScript, it will run properly.
This is not a valid su command. From the su manpage:
Code:
-, -l, --login
Provide an environment similar to what the user would expect had the user logged in directly.
When - is used, it must be specified as the last su option. The other forms (-l and --login) do not have this restriction.
So I would recommend to change it to one of these:
Code:
su -l MyUser -c "/script/MyScript.sh"
su --login MyUser -c "/script/MyScript.sh"
su MyUser -c "/script/MyScript.sh" -
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.