Title
How do I call the "Caffeinate" shell script command and move on with the rest of my script?
What do you expect to happen when you run the script/macro?
My goal is to use these command lines to activate "caffeinate" in a script that is running for a long time (overnight, etc). The idea would be that I could run "caffeinate" and then the computer would not sleep until I call "pkill caffeinate" at the end of the script.
Whenever I run this, the script just hangs on the first "caffeinate" call indefinitely. Is there a way to call "caffeinate" and then move on with the rest of the javascript?
Thanks!
Are you seeing an error?
What happens when you run this script?
Whenever I run this, the script just hangs on the first "caffeinate" call indefinitely. It never moves to the next line of code.
How were you running this script?
I used a keyboard shortcut while SoundFlow was focused
How important is this issue to you?
3
Details
{ "inputExpected": "My goal is to use these command lines to activate \"caffeinate\" in a script that is running for a long time (overnight, etc). The idea would be that I could run \"caffeinate\" and then the computer would not sleep until I call \"pkill caffeinate\" at the end of the script. \n\nWhenever I run this, the script just hangs on the first \"caffeinate\" call indefinitely. Is there a way to call \"caffeinate\" and then move on with the rest of the javascript?\n\nThanks!", "inputIsError": false, "inputWhatHappens": "Whenever I run this, the script just hangs on the first \"caffeinate\" call indefinitely. It never moves to the next line of code.", "inputHowRun": { "key": "-MpfwjqdCPWPz1IbQT3I", "title": "I used a keyboard shortcut while SoundFlow was focused" }, "inputImportance": 3, "inputTitle": "How do I call the \"Caffeinate\" shell script command and move on with the rest of my script?" }
Source
sf.system.exec({
commandLine:`caffeinate -di`,
})
log ("Caffeinated")
sf.wait({ intervalMs: 200 });
sf.system.exec({
commandLine: `pkill caffeinate`
})
log ("uncafeinated");
Links
User UID: oMDTMcbKv2OT4GaE3NFzdjMdeXj2
Feedback Key: sffeedback:oMDTMcbKv2OT4GaE3NFzdjMdeXj2:-MuWHkvCubi5JkQEMiau
Linked from:
- Chad Wahlbrink @Chad2022-01-28 15:57:13.772Z
This workaround seems to do the job for now, but if there is a way to do it without using the terminal at all, that'd be excellent!
// Launch Terminal sf.app.launch({ path: "/System/Applications/Utilities/Terminal.app", }); sf.ui.app('com.apple.Terminal').appWaitForActive(); // Call "Caffeinate" sf.keyboard.type({ text: "caffeinate -di &", }); sf.keyboard.press({ keys: "return", }); // Hide Terminal sf.ui.app('com.apple.Terminal').menuClick({ menuPath: ["Terminal","Hide Terminal"], }); log ("Caffeinated") // Wait for Code sf.wait({ intervalMs: 2000 }); // Kill "Caffeinate" sf.system.exec({ commandLine: `killall caffeinate` }) // Quit "Terminal" sf.ui.app('com.apple.Terminal').appQuit({ waitForTermination: true, }); log ("Un-caffeinated");
- In reply toChad⬆:Kitch Membery @Kitch2022-01-30 00:13:02.614Z
Hi @chadwahlbrink,
This is completely untested, but give this a go.
function doWithCaffeinate(callback) { //Caffeinate here sf.system.exec({ commandLine: `caffeinate -di`, }); try { callback(); } finally { //De-caffeinate Here sf.system.exec({ commandLine: `pkill caffeinate` }); } } doWithCaffeinate(() => { //Add code you want to run while Caffeinated, here. });
Here I'm using a callback function in conjunction with a try => finally block.
Let me know if it works. :-)
Kitch Membery @Kitch2022-01-30 00:16:20.324Z
If this doesn't work let me know. I have a possible solution if it hangs on the
caffeinate -di
line of code.Chad Wahlbrink @Chad2022-01-30 03:13:12.476Z
Hey @Kitch !
I think this still gets stuck at
sf.system.exec({ commandLine: `caffeinate -di`, });
It does give me more reason to learn about callback functions and try => finally, though!
I'd be curious if you have another possible solution for the hanging code, but I'll also explore more on my own. Would making it a separate script work? Like, could I call a separate script to run in the background while I run the main script?
For more, context, this is for the Bounce-In-Batch script. I ran it overnight the other night and my computer fell asleep. It picked up where it left off when the computer woke up. So I'm just trying to figure out the best way to temporarily avoid sleep settings.
Kitch Membery @Kitch2022-01-30 03:21:45.636Z
Yes that is what I was going to suggest.
Create two new scripts in the same package as the bounce script;
- Caffeinate
sf.system.exec({ commandLine: `caffeinate -di`, });
- De-caffeinate
sf.system.exec({ commandLine: `pkill caffeinate` });
Then use the same callback function setup in the bounce script like this;
function doWithCaffeinate(callback) { //Caffeinate here sf.soundflow.runCommand({ commandId: "Enter Command ID Local to package for Caffeinate here", executionMode: "Background", 'async': true, }); try { callback(); } finally { //De-Caffeinate Here sf.soundflow.runCommand({ commandId: "Enter Command ID Local to package for De-affeinate here", executionMode: "Background", 'async': true, }); } } doWithCaffeinate(() => { //Add code you want to run while Caffeinated, here. });
Let me know if that works :-)
- In reply toChad⬆:
Kitch Membery @Kitch2022-01-30 03:28:28.611Z
@chadwahlbrink... Again it's not tested but the logic seems sound... to me :-)
Chad Wahlbrink @Chad2022-01-30 14:29:09.847Z
Hey @Kitch!
Thanks for this code! I am fascinated by using callbacks, try, finally. I am still researching the use of all of those.
However, I think I cracked it differently.
This was my test setup:
// Caffeinate and Timeout setTimeout(() => {sf.system.exec({ commandLine: `caffeinate -di`, })}, 200) // Script log ('caffeinated'); sf.wait({ intervalMs: 300000 }); // Decaffeinate sf.system.exec({ commandLine: `pkill caffeinate` }); log ('decaffeinated');
↓The barebones code pieces are ↓
// Caffeinate and Timeout setTimeout(() => {sf.system.exec({ commandLine: `caffeinate -di`, })}, 200) // ✨✨✨Put your beautiful script here ✨✨✨ // Decaffeinate sf.system.exec({ commandLine: `pkill caffeinate` });
Thanks for your help on this! I'm glad I was able to get something to work. I'm not familiar with the setTimeout() function, but it seemed like a way to break out of the "caffeinate" loop after some googling.
It seems to work well! I set my computer sleep settings to 1 minute, and the caffeinate command does keep the computer chugging along 🚂
Now, I'm going to get a little bit of caffeine myself ☕️✌️