Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to launch a script bash from another one using sudo.

Here is my first script test.sh

#!/bin/bash
echo "test.sh" $1 $2 $3  >>/home/pi/test.log
sudo ./temp.sh "$1" "$2" "$3" >>/home/pi/test.log &
echo "test.sh done!" >>/home/pi/test.log

When I run

./test.sh a b c

in my console, it works. The second script is launched with args! But when the first script is launched from an other tools (Deluge Execute plugin), the second script is never launched. It looks like a problem with permissions.

Update : I rename the second script to temp.sh and its make an echo in a second file

#!/bin/bash
torrentid=$1
torrentname=$2
torrentpath=$3

echo " Details: " $torrentid $torrentpath $torrentname >> /home/pi/temp.log

It works when launched by command line, but not by Deluge.

Update 2 : The second script is voluntary launch in a second shell/env with & because it will make a long action. I do this because when a script is launched & executed by deluge plugin, deluge is freezed during the execution. So I need a script that launches a second one in background.

Update 3 : my user pi as already pi ALL=(ALL) NOPASSWD: ALL in the visudo file

share|improve this question
2  
Are you sure that Deluge Execute plugin sets the working directory correctly? Since you're referencing sort.sh relatively to the working directory, it may not exist at all. –  user2313067 Mar 30 at 13:53
    
Both scripts are in the same folder. If I target sort.sh from deluge, it's works. –  RaspDealer Mar 30 at 14:06
2  
How do you conclude that sort.sh doesn't run? Could you elaborate on that? Does the last statement, i.e. echo done execute? –  devnull Mar 30 at 14:18
1  
The "test.sh done!" line will right now be written to the log directly after test.sh has forked; not when the command is actually done. You should rather have a second line like: (sudo ./temp.sh "$1" "$2" "$3" && echo "test.sh done!") >>/home/pi/test.log &. You still won't be able to provide credentials for sudo non-interactively, so you will need to give the Deluge user sudo privileges to execute test.sh without password authentication for your attempt to work. –  Daniel Andersson Apr 28 at 14:15
1  
@RaspDealer: You can set such privileges in the sudo configuration file via the visudo command. Read up on the syntax. There might also be annotated examples and further instructions in the file when it opens. –  Daniel Andersson Apr 28 at 19:58

5 Answers 5

Try this:

#!/bin/bash
cd "$(dirname "$0")"
# rest of script goes here
# ...
share|improve this answer
    
Whats does this line ? –  RaspDealer Apr 28 at 12:01
    
You explicitly invoke ./temp.sh -- you specify "find temp.sh in the current directory". When you invoke test.sh, what is your current directory? Are you in the directory containing temp.sh? If not, you'll get an error like ./temp.sh: command not found. If you (or deluge) calls "/home/raspdealer/bin/test.sh", then your current directory will change to "/home/raspdealer/bin", and then "./temp.sh" will be found. –  glenn jackman Apr 28 at 15:07
    
Everything is in the same directory. –  RaspDealer Apr 28 at 17:41
    
That doesn’t matter, @RaspDealer. It’s all about the working directory. Never use relative paths when referring to “fixed” objects. –  Daniel B Apr 30 at 8:47

There are two problems with your BASH code:

  1. you use sudo which requires authentification, something Deluge Execute probably can't do (I don't know Deluge.)
  2. you use & which explicitly starts your command in a subshell with its own environment.

To fix 1. you have two options. You can either make the Deluge user a sudoer and echo the password into sudo like so echo $PASSWORD | sudo -S <command> or you can create an appropriate rule in your sudoers file to allow the Deluge user to run this command without authentication. Read the sudo man-page for more info.

To fix 2. just remove the "&" symbol and consider using the built-in command source. Read the BASH man-page for more info.

share|improve this answer

One definitive problem with

sudo ./sort.sh "$1" "$2" "$3" & >>/home/pi/test.log

is that you attempt to redirect the output after making the script run in background. Note that & acts as a command separator too, so you are essentially executing the following two commands:

sudo ./sort.sh "$1" "$2" "$3" &
>>/home/pi/test.log

You need to fix the order of the two. Say:

sudo ./sort.sh "$1" "$2" "$3" >>/home/pi/test.log &

Another problem is that you specify a relative path for the second script. Either replace ./temp.sh in the script with the absolute path or prefix it with the directory containing the script

"$(dirname "$0")"/temp.sh
share|improve this answer
    
He could group all the commands with { ... } and redirect the output of the group. Or exec 1>logfile first. –  glenn jackman Mar 30 at 14:24
    
@glennjackman True. It's tough to read into the mind of the OP. I just pointed to an obvious problem that would cause the redirection to be a no-op that might lead one into thinking that the command isn't being executed. –  devnull Mar 30 at 14:25
    
Thanks for that but this problem matters without the & too... –  RaspDealer Mar 30 at 14:35
    
I've just update the post –  RaspDealer Mar 30 at 15:03
    
Problem persists ! –  RaspDealer Apr 28 at 17:45

Another issue is that two commands (sudo ./temp.sh "$1" "$2" "$3" and echo "test.sh done!") are writing to the same file (/home/pi/test.log) concurrently (i.e., simultaneously), because the temp.sh (if it runs) has the file open in the background when the echo executes.  This shouldn’t be a major problem – it shouldn’t prevent temp.sh from running – but if you do solve the primary problem and get temp.sh to run, and it writes output, then it will probably overwrite the "test.sh done!" message.  Conversely, if temp.sh runs quickly enough, then the echo could possibly overwrite the first 14 bytes of output from temp.sh.

share|improve this answer
    
I'm ok with that but my sure that ./temp.sh wille take few minutes. I just need to export this 'action' in an other script to release the first one. –  RaspDealer Apr 28 at 12:02

Try this Modified scripts.
Test.sh

#!/bin/bash
cd "$(dirname "$0")"
echo "test.sh $1 $2 $3"  >>/home/pi/test.log
gksu bash ./temp.sh "$1" "$2" "$3" >> /home/pi/test.log &
echo "test.sh done!" >>/home/pi/test.log

temp.sh

#!/bin/bash
torrentid="$1"
torrentname="$2"
torrentpath="$3"

echo "Details:  $torrentid $torrentpath $torrentname" >> /home/pi/temp.log

If gksu not available use pkexec

comments

gksu is the graphical frontend of sudo. Just try sudo gedit and gksu gedit. Sudo removed from the newer version of Ubuntu. Did you tried the script ? How is it ?

2

try 1: add sleep 15 at the end of first script, test.sh.
Try 2: add nohup before gksu. Ie nohup gksu bash ...

share|improve this answer
    
Whats does gksu bash and pkexec bach ? –  RaspDealer Apr 28 at 12:04
    
I've got this message "Refusing to render service to dead parents." when I launch it manually –  RaspDealer Apr 28 at 17:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.