Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scripting Switch User 1

Status
Not open for further replies.

olli2003

Technical User
Jan 31, 2003
93
0
0
DE
Hi Gys!
It would be nice to get some suggestions for my problem here. I'd like to execute a command by another user than Root. This should be done by a script. The process within the script must be like this:

ROOT#su - <USER>
USER#ls -ltr /opt/freeware/glitter/*.log > /tmp/glitter.txt

Please notice that I have a different user environment. Perhaps I should directly do in the crontab? I've tried to do with this user, but it doesn't work :(

Thanks for all help!
Kind Regards, Oliver
 
Not sure i have fully understood your problem but may be "sudo" is your answer.
 
Do you know why this user's crontab is not working?
Any errors shown?
I would try to see why it doesn't work, because it would be the best way to do it.

But if you're not able to make it work this way, maybe you can try this script from root:
Code:
su - username "-c ls -ltr /opt/freeware/glitter/*.log > /tmp/glitter.txt"

HTH
 
Hi!
Thanks for all solutions! But what on earth is "Sudo"? I've never heard before. So at last the script offered by MoreFeo works very well:) But just one more little problem, I've started a process within this script, too.

It looks like this:

root 262314 172134 0 Jun 10 - 0:00 /usr/sbin/writesrv

The process hangs and doesn't stop automatically. Well, is it possible to kill with f. ex. the kill -9 command? Is it possible to kill a process without knowing the PID? I'd like to kill it by looking for the string. In this example "kill /usr/sbin/writesrv"
Thanks a lot again!
Kind Regards
Oliver
 
You should grep for the process name, and from there take the pid and then kill this pid.
 
To elaborate: grep for the process name using ps -ef | grep <name>

But why is it 'hanging' in the first place?

The internet - allowing those who don't know what they're talking about to have their say.
 
Hi!
Yes I know to grep for the process, but my problem is: How can I take out the PID there to chain this with the kill command? Is it complicated, isnt it? Well, the process hangs cause it's a running programm which wait for several characters or inputs to close in a normal way. So at last it's easier to kill the process on OS side, I think!?

Kind Regards
Oliver
 
Something like:
Code:
PID=$(ps -ef | grep "/usr/sbin/writesrv" | awk '{print $2}')
kill -9 $PID

I don't have a server to try, but something like this should work.
 
sudo" allows a permitted user to execute a command as the superuser or another user, as specified in the sudoers file.
 
By the way, if you're going to try the solution I posted be careful, as it doesn't check for non existent processes, or if there is more than one result.

You should try before replacing the kill -9 by an echo command, to see if it shows the correct PID.
 
Hi!
Thanks a lot, you're great! Well, I've tested in a script, and the current process will hang up while the script is hanging. I've tried to find the solution therefore. I've thinked about - at all - I can't fetch a PID within the script which is the PID of itself. It's right? If I cancel the execute with the <STRG>C keys, the process I want will canceled, too. Is there a command I can implement in it to stop itself? That must be ok then!

Kind Regards
Oliver
 
Hey,
I know, I'll manage it with a second script (killscript) for cancel the main once. But I'll get an error message by execute:

./killscript: {ps: not found
awk: There is an extra } character.
The source line is 1.
The error context is
{print >>> $2}} <<<
syntax error The source line is 1.
awk: There is an extra } character.
awk: Quitting
The source line is 1.
./killscript[2]: kill: bad argument count

Can you help me some more time, please? Thanks much!

Kind Regards
Oliver
 
Well, without the benefit of having a look at your killscript...
==> SHOW IT TO US PLEASE!
I'll have a guess:

[tt]{ps[/tt] must be just [tt]ps[/tt]

and also lose the last [tt]}[/tt]


HTH,

p5wizard
 
Hey!

No problem, the process I like to kill ist this:

root 286914 86172 0 Jan 21 - 0:00 /usr/sbin/writesrv

and the killscript is like that:

PID=$(ps -ef | grep "/usr/sbin/writesrv" | awk '{print $2}')
kill -9 $PID
exit 0

Thanks & Kind Regards
Oliver
 
Try this:-

#!/usr/bin/ksh
#
PID=$(ps -ef | grep "/usr/sbin/writesr[v]" | awk '{print $2}')
kill -9 $PID
exit 0

The "[" will stop you pulling up the grep line on the ps command so you get the line you want.
 
Hi &
Sorry, now I'm a little bit better, I've canceled the last { like this.

The Script:

#!/usr/bin/ksh
#
PID=$ ps -ef |grep "/usr/sbin/writesrv" | awk '{print $2}'
kill -9 $PID
exit 0


Now I'll get the right PID, but still have a failure in the script.

The Output:

papsol:root:/opt/scripts # ./killscript
630870
./killscript[4]: kill: bad argument count
papsol:root:/opt/scripts # ps -ef |grep /usr/sbin/writesrv
arasol 630870 2826254 0 09:06:54 pts/1 0:00 -csh -c /usr/sbin/writesrv > /tmp/glitter.txt


Thanks a lot!
Kind Regards
Oliver
 
READ THIS FIRST!

Well, in the first place you shouldn't be killing and restarting this process at will, it is controlled by the system controller master like so:

stopsrc -s writesrv
(wait 15s or so until it stops)
lssrc -s writesrv
(shows inoperative state)
startsrc -s writesrv

=====================================================


Now to get back to your stop script:

Earlier on you talked about this error:
olli2003 said:
./killscript: {ps: not found
awk: There is an extra } character.
The source line is 1.
The error context is
{print >>> $2}} <<<
syntax error The source line is 1.
awk: There is an extra } character.
awk: Quitting
The source line is 1.
./killscript[2]: kill: bad argument count

Hinting at a { and a } too many, but then you show the script and it looks sort of all right to me - awk program between '{ and }' and shell pipeline between $( and ):

olli2003 said:
PID=$(ps -ef | grep "/usr/sbin/writesrv" | awk '{print $2}')
kill -9 $PID
exit 0

It is not ideal, as the script starts a grep process which in itself also has the string you're looking for so it is arbitrary if your script will find one or two PIDs. So vodkadrinker suggested a correction:

vodkadrinker said:
#!/usr/bin/ksh
#
PID=$(ps -ef | grep "/usr/sbin/writesr[v]" | awk '{print $2}')
kill -9 $PID
exit 0

The $( ... ) is needed to get the output from the command between $( and ) assigned to the PID variable. So you can't remove the ( and the ) !

And here's another solution, without the grep as awk can also look for words, it just gets a little bit tricky if your search words contain the / character, as you have to escape or quote this character:

Code:
#!/usr/bin/ksh
#
PID=$(ps -ef | awk '/csh -c \/usr\/sbin\/writesrv/ {print $2}')
if [ "${PID}" != '' ]
then
 kill -9 $PID
fi
exit 0


HTH,

p5wizard
 
Okay, I've a better understanding.
Thanks a lot for your suggestions and solutions.
Was a good help to me!

Kind Regards
Oliver
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top