LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-03-2015, 02:29 AM   #1
QWE123
Member
 
Registered: Nov 2014
Posts: 55

Rep: Reputation: Disabled
about su user to run schedule job


I have a script , the content is as below .

#vi my_script

Code:
su - userA

if ..
then 
..
else

the script do the command "su" to another user first then run the following command , but I found that the output is difference to directly su to the user on the shell , and then run the script , as below .

# su - userA
$ whoami
userA
$ my_script


would advise if I want to use first method - embed the su in the script , but have the SAME ouput as the second method , how to do it ? thanks
 
Old 07-03-2015, 03:10 AM   #2
cliffordw
Member
 
Registered: Jan 2012
Location: South Africa
Posts: 509

Rep: Reputation: 203Reputation: 203Reputation: 203
Hi there,

Putting a sequence of commands into a script isn't quite the same as typing the same sequence on the command line.

In this case, if you run your sequence manually, you're running the "whoami" command inside "su". In the script form, the "su" completes (and returns you to the root user) before running the next command.

One easy solution might be to do the su outside of the script, i.e. use the command:
Code:
su - userA /path/to/my_script
in your schedule.

Alternately, if you have to do the "su" from within the script, something like this might help:
Code:
#!/bin/bash
MYUSER="userA"                                                                                                                      
if [ "$USER" != "$MYUSER" ]
then
    /usr/bin/su - $MYUSER $0 $*
    exit $?
fi

# Rest of original script goes here...
if ..
then 
..
else
..
fi
Note that you'll have to call my_script with a full path, or hard code the path on line 5 above instead of $0 for this to work, as you'll be in a different directory after the "su".

Good luck!
 
Old 07-03-2015, 03:34 AM   #3
JaseP
Senior Member
 
Registered: Jun 2002
Location: Eastern PA, USA
Distribution: K/Ubuntu 18.04-14.04, Scientific Linux 6.3-6.4, Android-x86, Pretty much all distros at one point...
Posts: 1,802

Rep: Reputation: 157Reputation: 157
Is the following what you are trying to do???

http://superuser.com/questions/17086...-specific-user
 
  


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
Run cron job with different User kunalks Linux - Server 7 06-19-2014 11:18 PM
[SOLVED] Run a job as different user sras Solaris / OpenSolaris 3 08-14-2013 03:15 AM
crontab - schedule job to run every minute Hi_This_is_Dev Linux - General 18 04-28-2010 12:52 PM
how to run a job as a different user from crontab digitallyamar Linux - Server 2 09-13-2009 06:39 AM
schedule job to be run at 8:00pm every night ashley75 Linux - General 22 10-24-2003 12:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:03 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