I am making a script where I wish to run two services as a certain user. The script should switch to a user and then run a program and after the program has been executed, it should start running the second program.
sudo command can be told to run as a specific user:
-u user, --user=user Run the command as a user other than the default target user (usually root). The user may be either a user name or a numeric user ID (UID) prefixed with the ‘#’ character (e.g. #0 for UID 0).
So, inside your script, you can use
sudo -u USERNAME to run the two services:
sudo -u foo command1 sudo -u foo command2
However, this means that the script itself will need to be run as root. Otherwise, the
sudo will ask for the password of the user