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.

Tak
  • 976
  • 3
  • 16
  • 35

1 Answers1

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.

muru
  • 207,228