0

I have 5 shell files in a specific locations on my ubuntu system. I have two users on this system userA which is the main user and userB. Currently I have to manually open a terminal using user userA and then run a sequence of some terminal commands which includes running 4 of the 5 shell files I have, then I open another terminal and switch to userB and then run a sequence of some commands and 1 shell file. What I want to do is to have two shell files on the desktop (e.g. userA.sh and userB.sh) and when I double click on the userA.sh it will open a terminal using userA and then run the sequence of commands which includes running four shell files, and when I double click on the userB.sh it will open another terminal and switch to userB and then run a sequence of some commands which includes one shell script.

EDIT:

This is what I did according to Muru suggestion: I created a scriptA.sh on the desktop and make it executable then this was it content:

#!/bin/bash
sudo -u userA bash -c 'PACKAGE_PATH=/home/userA/package1; cd /home/userA/scripts'

but after double clicking it and choosing to run with terminal option, the terminal opens for a second and then closes itself. I don't know why this is happening, so I'd appreciate if anyone could please advise why this is happening and how to overcome it.

2
  • Do these scripts have to be run as those users?
    – muru
    Sep 22, 2014 at 8:49
  • thanks for your comment! yes, so there are 10 commands or scripts (four of them is running another .sh files) that are run on the main ubuntu user using terminal. Then there are 5 other commands or scripts (one of them is running one .sh file) that run in another terminal using another user, which means that the first command of these 5 commands is a su command
    – Tak
    Sep 22, 2014 at 8:59

1 Answer 1

1

You'll have to use sudo somewhere. Lets use them in the scripts:

#! /bin/bash
#userA.sh

sudo -u userA bash -c '/path/to/script1.sh; /path/to/script2.sh; ... \
    /path/to/scriptn.sh; 

Similarly for userB. Consider creating a .desktop file for both scripts, or making them run on double click. Then consider giving yourself password-less perissions for both scripts:

sudo tee -a /etc/sudoers.d/user-scripts <<EOF
$USER ALL = (ALL) NOPASSWD: /path/to/userA.sh, /path/to/userB.sh
EOF

If you do want to enter the passwords, get them to run in a terminal (depends on how you execute the script), or use pkexec instead of sudo.

3
  • thanks for your answer. so if there are other commands beside running the other shell files lets say a cd command, will it be like this sudo -u userA bash -c 'cd .. ; /path/to/script1.sh; /path/to/script2.sh; ... \ /path/to/scriptn.sh; ? And if yes, then what will be the case in userB.sh? as the first command is su which asks for the password of this user, how to deal with this? Also couple of the commands inside requires to enter the password of a specific application, how this can be handled?
    – Tak
    Sep 22, 2014 at 9:18
  • 1
    @user1460166 yes, the commands can be whatever you enter in the terminal, separated by ; or by the escaped newline (\ followed immediately by enter). For userB, you use sudo -u userB bash -c '...'. You can make it run in a terminal in both methods, in which case, you can enter the password in the relevant terminals.
    – muru
    Sep 22, 2014 at 9:20
  • could you please check my other question here askubuntu.com/questions/527296/… ?
    – Tak
    Sep 23, 2014 at 5:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .