Execute one command after another one finishes under gksu

1.4k Views Asked by At

I'm trying to have a desktop shortcut that executes one command (without a script, I'm just wondering if that is possible). That command requires root privileges so I use gksu in Ubuntu, after I finish typing my password and it is correct I want the other command to run a file. I have this command:

xterm -e "gksu cp /opt/Popcorn-Time/backup/* /opt/Popcorn-Time; /opt/Popcorn-Time/Popcorn-Time"

But Popcorn-Time opens without it waiting for me to finish typing my password (correctly). I want to do this without a seperate script, if possible. How should I do this?

EDIT: Ah! I see what is going on now, you've all been helping me with causing Popcorn-Time to wait for gksu to finish, but Popcorn-Time isn't going to run without the files in backup, and those are a bit heavy (7 MB total), so it takes a second for them to complete the transfer, then Popcorn-Time is already open by the time the files are copied. Is there a way to wait for Popcorn-Time to wait for the cp command to finish? I also changed my command above to what I have now.

EDIT #2: Everything I said by now isn't relevant, as the problem with Popcorn-Time isn't what I thought, I didn't need to copy the files over, I just needed to run it as root for it to work. Thanks for everyone who tried to help.

Thanks.

3

There are 3 best solutions below

9
On

In a console you would do:

gksu cp /opt/popcorntime/backup/* /opt/popcorntime; /opt/popcorntime/Popcorn-Time

In order to use it as Exec in the .desktop file wrap it like this:

bash -e "gksu cp /opt/popcorntime/backup/* /opt/popcorntime; /opt/popcorntime/Popcorn-Time"
8
On

The problem is that gnome-terminal is only seeing the gksu command as the value to the -e argument and not the Popcorn-Time command.

gnome-terminal forks and returns immediately and so Popcorn-Time runs immediately.

The solution is to quote the entire command string (both commands) so they are (combined) the single argument to -e.

9
On

If you want the /opt/popcorntime/Popcorn-time command to wait until the first command finishes, you can separate the commands by && so that the second only executes on successful completion of the first. This is called a compound-command. E.g.:

command1 && command2

With gksu in order to run multiple commands with only a single password entry, you will need:

gksu -- bash -c 'command1 && command2'

In your case:

gnome-terminal -e gksu -- bash -c "cp /opt/popcorntime/backup/* /opt/popcorntime && /opt/popcorntime/Popcorn-Time"

(you may have to adjust quoting to fit your expansion needs)

You can use the or operator in a similar fashion so that the second command only executes if the first fails. E.g.:

command1 || command2