LinuxQuestions.org
Register a domain and help support LQ
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 03-19-2011, 07:33 AM   #1
linux_beginer
LQ Newbie
 
Registered: Mar 2011
Posts: 24

Rep: Reputation: 0
Script which will login to particular user and execute some commands.


Hi All,

I am trying to write one script. Purpose of my script is that it will login to particular user and it will execute some set of commands.

What I was trying....


#!/bin/bash

su - tom
ls -lrt
gzip *.dbf


but it is not working properly. what happening is I am getting the shell of user tom but but commands are not getting executed.

I am running this script as root user. what should I have to do if I wanted to run this script as normal user. I want to pass the password in the script only. (--stdin will work)
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-19-2011, 09:28 AM   #2
savona
Member
 
Registered: Mar 2011
Location: Bellmawr, NJ
Distribution: Red Hat / Fedora
Posts: 195

Rep: Reputation: 50
When you su - tom it opens a new shell and the script it running in the original shell.

Try this.

#!/bin/bash
su tom -c "ls -lrt"
su tom -c "gzip *.dbf"

This will pass a command to the current shell as the user specified.
 
1 members found this post helpful.
Old 03-19-2011, 09:33 AM   #3
savona
Member
 
Registered: Mar 2011
Location: Bellmawr, NJ
Distribution: Red Hat / Fedora
Posts: 195

Rep: Reputation: 50
Quote:
Originally Posted by savona View Post
When you su - tom it opens a new shell and the script it running in the original shell.

Try this.

#!/bin/bash
su tom -c "ls -lrt"
su tom -c "gzip *.dbf"

This will pass a command to the current shell as the user specified.
I just wanted to add something to make sure you understand what is going on here. With the work around I gave each command will be run in its own shell instance.

Can I ask you would would want to do this anyway?
 
2 members found this post helpful.
Old 03-19-2011, 09:37 AM   #4
akamikeym
Member
 
Registered: May 2008
Posts: 107

Rep: Reputation: 21
Or just put the commands in script and run them as whatever user you want when you want them:

myscript.sh
Code:
#!/bin/bash
ls -lrt
gzip *.dbf
Then you can call it with:

su tom -c "sh myscript.sh"

Or

sudo -u tom sh myscript.sh
 
3 members found this post helpful.
Old 03-19-2011, 09:40 AM   #5
savona
Member
 
Registered: Mar 2011
Location: Bellmawr, NJ
Distribution: Red Hat / Fedora
Posts: 195

Rep: Reputation: 50
Quote:
Originally Posted by akamikeym View Post
Or just put the commands in script and run them as whatever user you want when you want them:

myscript.sh
Code:
#!/bin/bash
ls -lrt
gzip *.dbf
Then you can call it with:

su tom -c "sh myscript.sh"

Or

sudo -u tom sh myscript.sh

GREAT POINT! There is always more than one way to skin a cat with linux.
 
2 members found this post helpful.
Old 03-20-2011, 02:05 PM   #6
linux_beginer
LQ Newbie
 
Registered: Mar 2011
Posts: 24

Original Poster
Rep: Reputation: 0
!!!Thanx savona!!! Problem is solved

Quote:
Originally Posted by savona View Post
GREAT POINT! There is always more than one way to skin a cat with linux.
!!!Thanx savona!!! Problem is solved
 
Old 03-20-2011, 02:06 PM   #7
linux_beginer
LQ Newbie
 
Registered: Mar 2011
Posts: 24

Original Poster
Rep: Reputation: 0
!!!Thanx akamikeym!!! Problem is solved

Quote:
Originally Posted by akamikeym View Post
Or just put the commands in script and run them as whatever user you want when you want them:

myscript.sh
Code:
#!/bin/bash
ls -lrt
gzip *.dbf
Then you can call it with:

su tom -c "sh myscript.sh"

Or

sudo -u tom sh myscript.sh
!!!Thanx akamikeym!!! Problem is solved

---------- Post added 03-20-11 at 01:07 PM ----------

Quote:
Originally Posted by akamikeym View Post
Or just put the commands in script and run them as whatever user you want when you want them:

myscript.sh
Code:
#!/bin/bash
ls -lrt
gzip *.dbf
Then you can call it with:

su tom -c "sh myscript.sh"

Or

sudo -u tom sh myscript.sh

Last edited by linux_beginer; 03-20-2011 at 02:07 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Execute script on local server as normal user to run commands on remote server ALInux Linux - Software 1 01-01-2010 07:30 AM
cannot execute some commands as an plain user! minike Slackware 5 09-03-2004 07:34 PM
how can execute some commands when a user logs out rddreamz Programming 2 05-25-2004 04:00 PM
Can't execute commands when logged in as ordinary user.... murphaph Linux - General 3 04-05-2004 06:23 PM
how to execute commands on login k4zau Linux - Software 3 10-12-2003 02:52 PM


All times are GMT -5. The time now is 02:19 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration