Using QProcess to run shell script
Hi! I'm currently working on a plugin that needs to run a shell script within it to run a set of python commands for some audio processing tasks. I've written the following function, where the run_script.sh
is the shell script that should be run (and I've been passing in absolute paths, since I'm just trying to get it to work on my local machine for now). I've tried both the commented out start
line just calling the source
command as well as the other start
call that actually uses bash
, and while the first one completely doesn't work (says that it failed), the second one completely freezes MuseScore.
// Run python script for audio transcribing
function runCommand(){
proc.start("pwd");
proc.waitForFinished(300000);
console.log(proc.readAllStandardOutput());
// proc.start("source /path/to/script/run_script.sh");
proc.start("bash", ["-c", "source \"\"/path/to/script/run_script.sh\"\""]);
var venv = proc.waitForFinished(-1);
console.log(venv);
console.log("Output", proc.readAllStandardOutput());
// console.log("running command", cmd);
var args = new Array();
}
Does anyone have input as to why it would freeze upon trying to run the bash
command? Is there anyway to prevent this from happening/ get it to actually run the right shell script, or is that not feasible given how QProcess has been incorporated into MuseScore?
This is also my first time writing a plugin (but have been looking through forums and QML documentation), so apologies if I'm misunderstanding anything. Thanks so much!
Comments
Why that double
\"\"
? Once should be enough, shouldn't it?In reply to Why that \"\"? by Jojo-Schmitz
That's a good point; I had seen somewhere on some QML forum that there should be 2, but now that I try to find the forum post again, I can't find it. With that being said, since then, I've tried:
proc.start("bash", ["-c", "source \"/path/to/script/run_script.sh\""]);
proc.start("bash", ["-c", "source", "/path/to/script/run_script.sh"]);
And both of these have crashed musescore.
If it maybe helps, the current bash script looks like:
as a test just to see whether I can get something running, and it works in my regular terminal.
In reply to That's a good point; I had… by gh20
Try proc.start("bash", ["-c", "\"source /path/to/script/run_script.sh\""]);
In reply to Try proc.start("bash", ["-c"… by Jojo-Schmitz
Okay so the thing that actually ended up working was
proc.start('bash -c "source /path/to/script/run_script.sh"');
Similarly, to do a purels
command, I would doproc.start('bash -c "ls"')
With that being said, even though the thing doesn't freeze my musescore anymore, my shell script doesn't run. It contains a python script locally and in effect, does something like the following:
- cd to the right directory to activate the virtual environment
- runs the python script.
The bash command also doesn't work (no output, even when it just has an ls command in it) when trying to run the bash command with just the direct path to the python venv and running the script.
Both of these commands (the shell script and just the python call directly) work in my terminal (outside of the folder where they are located). I was wondering if you might know whether this is just a limitation with Musescore or whether I might just be doing something wrong again?
Thanks so much for your help so far!
In reply to Okay so the thing that… by gh20
There is the in the snippets library one snippet about running an external commend: start another program commandline. This may help you.
In reply to There is the in the snippets… by parkingb
I tried using
/bin/sh
, but the program still doesn't run :") it just says that it completed but nothing shows up.In reply to I tried using /bin/sh, but… by gh20
Try this:
And the script I used is
It is working fine. It is just a matter of finding the right quote combination !
In reply to Try this: d = '/bin/sh -c "… by parkingb
So I copied this exact code into my Musescore 3 (hopefully correctly, though I might've done something wrong there), and while the command says that it ran successfully (see the second screenshot), nothing actually gets printed. Is it due to my OS? I'm on a Mac (if that changes anything)
The script:
In reply to So I copied this exact code… by gh20
Sorry i didn't realize the screenshots don't preview:
and output:
In reply to Sorry i didn't realize the… by gh20
Your
is weird.
On my Ubuntu, the execution of that code returns some(*) content. I.e. a typical
ls
command output.So I guess, your shell script is not running.
Are your sure your
script.sh
file is located in~/Documents/MuseScore3/Plugins/
?If you launch it manually, does it write a date into
/tmp/date.txt
?(*): "some content" = the content of the folder
/tmp/.mount_MuseScIRyi0X/bin/
which is (I guess) where MuseScore is unpacked when ran as an AppImageIn reply to Your Debug: -- Command… by parkingb
Honestly, I wonder if it's because I'm on MacOS. The script.sh file is in that file path, and when I launch it manually, it writes a date into the correct file in the correct place. It's alright though; I was using this for a class project, and the fact that it just doesn't work on MacOS (at least on mine) is frustrating but not the worst thing. Thanks for all your support!
In reply to So I copied this exact code… by gh20
Does /tmp/date now exist? What's s and TS content?
In reply to Does /tmp/date now exist?… by Jojo-Schmitz
to the best of my knowledge, it does not exist, and the ls did not print anything.
In reply to to the best of my knowledge,… by gh20
I guess the question of Jojo is "when you launch the script manually, does it create the /tmp/date file ?".
If the answer is no, then check if "/tmp" exist, check its permissions, ...