LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-21-2013, 02:57 PM   #1
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Rep: Reputation: 49
Does a su carry through to a script called by another script


Say I have a cron'd script in root's crontab like this

1 1 * * * su - MyUser -c "/script/MyScript.sh"

and in MyScript.sh it runs AnotherScript.sh.

So MyUser will run in MyUser's environment/shell, but by default does that also mean AnotherScript.sh will too? or will that then run in root's?
 
Old 10-21-2013, 03:05 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
1 members found this post helpful.
Old 10-21-2013, 03:07 PM   #3
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
Thanks. That's how I took it too, but I guess I'll find out.

How would I echo that stuff out? I'm just trying to tweak some existing scripts I inherited.
 
Old 10-21-2013, 03:22 PM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.

Code:
echo $PATH >> /home/users/myuser/logs/logfile.txt
echo $USERNAME >> /home/users/myuser/logs/logfile.txt
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.
 
1 members found this post helpful.
Old 10-21-2013, 03:26 PM   #5
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
Very cool, I will have to give this stuff a try, thanks!
 
Old 10-21-2013, 03:55 PM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
why not just setup the MyUser's crontab?

Code:
su MyUser -c "crontab -e"
Code:
1 1 * * * /script/MyScript.sh
 
Old 10-21-2013, 03:57 PM   #7
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
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.
 
Old 10-21-2013, 04:00 PM   #8
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by rjo98 View Post
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.
then setup cron's env correctly

Code:
man 5 crontab
 
Old 10-21-2013, 04:01 PM   #9
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Quote:
Originally Posted by rjo98 View Post
su - MyUser -c "/script/MyScript.sh"
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" -
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Each time a script is called. A log file is created with time and date + Bash Script. y0_gesh Programming 6 08-17-2012 03:16 AM
A script called by another script would always give exit status other than Zero(0) vandana Linux - General 2 03-22-2010 04:26 AM
Shell script: can you tell if a script is called via an alias? BrianK Programming 11 10-01-2009 11:16 AM
how to pass a variable from a called script back to the calling script steven.c.banks Linux - General 2 05-05-2008 02:00 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration