how to run script? call other script? su to another user? make a cron?

Good morning. I am searching for "how-to"'s for some particular questions:

  1. How to write a script in HP-UX 11.

  2. How to schedule a script. [using cron job.]

  3. How to "call" scripts from the original script.

  4. How to su to another user from within a script.

This is the basics of what the script should do.
su to a user
go to a specific directory
execute a process to stop a driver,
then another process to start a driver
exit su
begin the same as above, for the next user, and so on.

[I know the gist of the steps, because I do them from command line, but I consulted with a person who works here, and he said you can't just script the exits all in one file, because it'll make the script quit. My idea, is that if we could somehow have the script call other scripts, that would work, right? I hope that you gurus can assist lil 'ol me]

I need a script that does this:

  1. call a second script, that does this:

  2. su to user_x

  3. cd to a specific directory

  4. run a specific process to stop a driver [process already made, only
    user_x can run it]

  5. run a specific process to start a driver [process already made, only
    user_x can run it]

  6. exit [to quit out of su for user_x]

  7. call a third script, that does this:

  8. su to user_y

  9. cd to a specific directory

  10. run a specific process to stop a driver [process already made, only
    user_y can run it]

  11. run a specific process to start a driver [process already made, only
    user_y can run it]

  12. exit [to quit out of su for user_y]

  13. call a fourth script, that does this: (you probably know where I'm
    going with this by now ...)

  14. su to user_z

  15. cd to a specific directory

  16. run a specific process to stop a driver [process already made, only
    user_z can run it]

  17. run a specific process to start a driver [process already made, only
    user_z can run it]

  18. exit [to quit out of su for user_z]

  19. end of script

  20. schedule script to run every morning at 5:30 AM using cron

I had looked around the forum, and I think I could do this so far:

script name: startdriver.sh

and it calls other scripts (something like this)

startdriver1.sh && startdriver2.sh && startdriver3.sh

I hope that someone can assist me. I don't know the syntax for writing
the scripts. I know the manual commands to type in during a session, but
not the scripting.

I know that we use HP-UX, and that we use cron for scheduling jobs.

I wish that I was more help.

If you need more information prior to assisting me, please give me a
command I can run to find out that information.

[I don't normally do Unix work. I want the script so that I don't have to manually type in the commands.]

If this forum is not the proper place for a beginner such as myself, please
direct me to the proper forum.

Thanks!

And if the second script fail the others are never called?

Why not simply have a script for each user called by root cronfile?
Then the syntax would be in the cron file:
su - <user> -c <your_users_script>

Please don't post homework's, post reported, see The UNIX and Linux Forums - Forum Rules
Use the search box on top right corner if you want to find the answers.

Actually, if any of the three scripts fail, the process fails.

I'm asking because I don't know how to do this.

This is what I know how to do:

su <username>
cd /path/path/path/path
driver1.stop
driver1.start
exit
su <username2>
cd /path/path/path
driver2.stop
driver2.start
exit
su <username3>
cd /path/path/path
driver3.stop
driver3.start

Are you saying that I can schedule them independently of each other?

Due to the nature of the program (drivers for some program to tie into a database), I am not sure if I can do them, except in this order.

No homework permitted in the forums. Thanks.

Reopened thread. User provided office email (not homework).

Please continue.

Create a user.list file having the user.name , stop command and start command.

# cat <<eof>>user.list
user1 /path/to/user1/stop.proccess /path/to/user1/start.proccess
user2 /path/to/user2/stop.proccess /path/to/user2/start.proccess
eof

Use a loop to read from user.list and to test(echo) each command.

# while read user stop start
    do 
       echo su $user
       echo $stop
       echo $start
       echo exit
    done < user.list

The output will be:

su user1
/path/to/user1/stop.proccess
/path/to/user1/start.proccess
exit
su user2
/path/to/user2/stop.proccess
/path/to/user2/start.proccess
exit

When you have everything as you like , just remove the echo on your loop.

Success.

Thanks for the assistance from both of you.

As I understand it, the "do while var1 var2 ..."
just assigns variables, while reading through a file, correct? And you can then substitute those variables later.

I saw another example [following your advice of researching more myself] where I see that I can specifically assign the variables.

So, it can be like "u=user1 w=stop1.script v=start1.script"

it was something with grouping, and you could assign all your variables in one line. Something like that. Obviously, I suspect that you understand this a lot better than I do.

Thanks. You've opened my eyes. I hope to begin testing today. Hopefully the admin isn't insecure about his job to not show me how he schedules the cron tasks! ... If I have issues with that, though, I'll try to read more FIRST, so my question will be a bit more knowledgeable, and show more effort on my part, instead of asking for a spoon fed, step-by-step answer.

Try to spend some time here on unix.com and after few months get back to your first post/thread and think over :wink:

Good morning. I have tried further, but am still unsuccessful.

The echo works, but it does not execute as expected.

From what I have read on the hp.com forums, this problem could be related to the .profile of the users that I am calling to execute the commands.

http://forums13.itrc.hp.com/service/forums/questionanswer.do?admit=109447627+1242136488365+28353475&threadId=38292

According to what I have read, this could be due to the fact that the user .profile is set up to load a certain user environment, but, I'm not understanding what they mean by changing this environment.

But, I did get the ttytype error that they are talking about "not a typewriter" or something to that effect during my experiments this morning.

I have attempted
su - usera -c commanda
su - userb -c commandb
su - userc -c commandc
what I get is that it appears to logon to each user's login environment (profile, as it were) but it only appears to actually run the command one time, at the very end.
At command prompt, I type it in this way (but of course, I would not be here, unless I wanted to script this.)
I tried a do loop, but it failed.

I tried concatenating scripts with an &, and they run, but I need the su portion, in order to have the permissions on the created file work, as the apparent usera, userb, userc need some perms on the files created as a result of running the script.
Example:
commanda& commandb & commandc
This works, but there is a file(s) created, and it is under "root" and I need the files created under usera, userb, userc, respectively. I saw something about chmod 4700 in the hp.com forums, but that made no sense to me, as I know of only three digits for chmod, so the fourth digit confuses me! What is the purpose of the 4700?

Once I had attempted this, the unix admin had to go back and delete the files wherever they were creted. (He was familiar with this problem, as he had dealt with it before.)

I also tried this:
creating three separate files
filea:
su - usera
commanda
exit
fileb:
su - userb
comamndb
exit
filec:
su - userc
commandc
exit

And concatenating them together on the command line:

filea & fileb & filec
and that didn't work, it appeared to log on to each user profile, and then only executed the commands for userc.

So, I'm back where I started, SOL.

have you tried to put in a script for root containing:

su - <user1> -c "<your_users_script>"
su - <user2> -c <your_users_script>
su - <user3> -c <your_users_script>

Since it would be batch you would have to test in .profile for interactive shell..
su user just changes UID, you execute "as" user
su - user reads .profile and so loads user's environment (and this includes terminal keyb stuff cron doesnt like...)
you would use between doublequotes after -c if you are to enter more than a command (typically a script that accepts start or stop for argument)

OK here is what I modified in /etc/profile
look for the following line:
# Be sure that VUE does not invoke tty commands
and add after the first if so it looks like:

 Be sure that VUE does not invoke tty commands

 if [ ! "$VUE" ]; then
   if [[ $- = *i* ]]; then  #tty interactive test (VBE - 030306)
   # set term if it's not set
.
.

then look for the matching if and place before my if so it looks like:

   fi                                      #tty interactive test
 fi                                        # if !VUE

# Leave defaults in user environment

This wil avoid all the cron error messages such as " Not TTY .."

Thanks for all of your help. Our unix admin is leaving. I guess I'll deal with this in due time, I'm going to seek training first, though.

I see nothing of the sort that you post in the .profile

This is what I see in the profile, as far as "terminal" setup. There is nothing else that I can find, with regards to "VUE" in the setup.

        # 
################################################################################
# @(#) $Revision: 72.2 $      
# Default user .profile file (/usr/bin/sh initialization).
# Set up the terminal:
 if [ "$TERM" = "" ]
 then
  eval ` tset -s -Q -m ':?hp' `
 else
  eval ` tset -s -Q `
 fi
 echo "Value of TERM has been set to \"$TERM\". "
 export TERM 
 stty erase "^H" kill "^U" intr "^C" eof "^D"
 stty hupcl ixon ixoff
 tabs
# Set up the search paths

I could post more, but it doesn't look necessary at this time. (If you say so, I will.)

thanks!

Hi,

I am facing exactly the same problem as you

my Server as to user

1) root

2) net1

i have to run process as follows

su net1
procees.sh &

when i try the above commands it runns successfully.

but when i include them in the shell script and execute them as a cron, the process does not starts . I am trying very hard to find a fix to this problem and let you know if it works out . Meanwhile if you you come down to any solution , please post it. It will be of great help

Thanks

I never figured out how to get this going natively.

I utilized a telnet scripting tool. "tst10". It provided what I needed, to get this going. It's not sophisticated enough to handle anything with variance, though. ... Well, then again, maybe it is, if you use it just right.

Only responding, as I see that I never mentioned that I got it going.