0

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.

shoham
  • 792
  • 2
  • 12
  • 30
  • 1
    If you run `gksu sleep 10; echo foo` in a shell does foo happen immediately or wait for the gksu/sleep to end? I'm going to guess that gksu forks and exits and so returns immediately without waiting for the spawned process. – Etan Reisner Aug 07 '14 at 21:53
  • @EtanReisner After I input my password it waits 10 seconds then echoes "foo". – shoham Aug 07 '14 at 21:54
  • Oh, of course, the problem is that gnome-terminal is only seeing the gksu as argument and not the other command and gnome-terminal **definitely** does fork and return. – Etan Reisner Aug 07 '14 at 21:57
  • You need to quote both of those pieces as one argument to `gnome-terminal`. – Etan Reisner Aug 07 '14 at 21:57

3 Answers3

1

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"
enrico.bacis
  • 30,497
  • 10
  • 86
  • 115
  • He didn't say it didn't work. He said it doesn't wait for the copy to finish first. – Etan Reisner Aug 07 '14 at 21:52
  • @EtanReisner Where have you read that? He said: *Popcorn-Time opens without it waiting for me to finish typing my password* – enrico.bacis Aug 07 '14 at 21:54
  • Which means the copy can't possibly have finished at that point since it hasn't even started. – Etan Reisner Aug 07 '14 at 21:56
  • gksu does wait it would seem but that's not the OP's issue. – Etan Reisner Aug 07 '14 at 21:58
  • He has to remove `gnome-terminal -e` as my answer shows.. otherwise another terminal will be spawned and the second command executed immediately – enrico.bacis Aug 07 '14 at 21:59
  • @enrico.bacis That was the first thing I tried, but turns out Ubuntu shortcut "Exec" property doesn't like multiple commands and just ignores the second one, so I tried the "gnome-terminal -e" way. – shoham Aug 07 '14 at 22:01
  • I mean, what I showed you is the right command to execute. Just wrap the entire one inside a `gnome-terminal -e` if you want to add it to Exec – enrico.bacis Aug 07 '14 at 22:02
  • Ok, sorry, my fault, I thought it was clear but maybe it could have been expressed better. I thought the `gnome terminal -e` part was implicit :) – enrico.bacis Aug 07 '14 at 22:04
  • `cp` and almost every other command us synchronous. This means that it waits for it to complete before executing the next one. The provided code do wait for cp to complete its work. If this doesn't work for you it means that your problem is not there. – enrico.bacis Aug 08 '14 at 08:31
1

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
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • I did this: `gnome-terminal -e "gksu cp /opt/Popcorn-Time/backup/* /opt/Popcorn-Time && /opt/Popcorn-Time"` and Popcorn-Time didn't run. What's weird is that I also replaced the `&&` with `||`, in both cases, Popcorn-Time didn't run. – shoham Aug 07 '14 at 22:29
  • Get rid of the quotes at the end and put them back before the `&&`. I believe quoting the whole string is giving your problems. If both must use `gksu`, then do `gksu -e "command_1" && gksu -e "command_2"` – David C. Rankin Aug 07 '14 at 22:52
  • See the additional edits. You will most likely need `gksu -e "command_1" && gksu -e "command_2"` if command_2 also requires root permission. – David C. Rankin Aug 07 '14 at 23:07
  • The second command doesn't require root privileges. And it still doesn't work (and it also asks me what do I want to run). – shoham Aug 07 '14 at 23:38
  • I have a feeling that we are running into quoting issues. I've looked at `man gnome-terminal` and `man gksu`. It is almost like you need `gksu gnome-terminal -e bash -c "cp /opt/popcorntime/backup/* /opt/popcorntime && /opt/popcorntime/Popcorn-Time"` instead of `gnome-terminal -e gksu -- bash -c "cp /opt/popcorntime/backup/* /opt/popcorntime && /opt/popcorntime/Popcorn-Time"` – David C. Rankin Aug 07 '14 at 23:47
  • It says: `invalid option -- 'e'` – shoham Aug 07 '14 at 23:58
  • The `-e, --command Execute the argument to this option inside the terminal` only applies to `gnome-terminal`. Let me see if I have a box with both `gnome-terminal` and `gksu` on it. Something isn't making sense here... – David C. Rankin Aug 08 '14 at 00:15
  • I'm having fits with `gnome-terminal`. I got my test to work with `xterm`. Try `xterm -e "gksu -- bash -c cp /opt/popcorntime/backup/* /opt/popcorntime && /opt/popcorntime/Popcorn-Time"` **NOTE the quoting** (I'm also having to do with over remote-x [ssh X11 forwarding], so that may cause issues as well) – David C. Rankin Aug 08 '14 at 00:26
  • For that matter, try your original command just substituting `xterm` for `gnome-terminal` -- I'm having problems with `gnome-terminal` waiting for input (the widow closes immediately. With `xterm` it waits for my input as expected! – David C. Rankin Aug 08 '14 at 00:34
0

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.

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • Oh wait, I was a bit too quick to judge if it were working, now it seems like Popcorn-Time just won't run. – shoham Aug 07 '14 at 22:13
  • And it's not a problem with Popcorn-Time, I changed it to: `gnome-terminal -e "gksu cp /opt/Popcorn-Time/backup/* /opt/Popcorn-Time; echo foo; sleep 10"` and it still won't show foo and won't wait 10 seconds so I could see it. – shoham Aug 07 '14 at 22:16
  • What do you see in the `/tmp/test` file if you run `gnome-terminal -e 'echo foo > /tmp/test; echo bar >> /tmp/test'`? – Etan Reisner Aug 07 '14 at 22:23
  • No such file or directory? – shoham Aug 07 '14 at 22:25
  • For `/tmp/test`? Does it work if you try it with `xterm` instead of `gnome-terminal`? What about if you use `-x` instead of `-e` to `gnome-terminal`? – Etan Reisner Aug 07 '14 at 23:16
  • Popcorn-Time does run if I do it with xterm but the first command doesn't work. – shoham Aug 07 '14 at 23:47
  • The original goal works with xterm but not with gnome-terminal? Does my echo test work with xterm but not gnome-terminal also? Then it sounds like gnome-terminal is broken to me. – Etan Reisner Aug 08 '14 at 01:41
  • According to @enrico.bacis gksu does not fork and does wait and your sleep test confirmed that too. So I don't see how, with the command you indicate in your edit, popcorn-time could be starting before the copy finishes. If gksu returns before the cp finishes then both @enrico.bacis and my/your tests were wrong and we are back to forcing the shell to wait on it. (Possibly with `wait`.) – Etan Reisner Aug 08 '14 at 17:22