Run a separate process with JXA and daemon

13 Views Asked by At

I have a JXA (JavaScript for Automation) script that needs to perform a series of actions. Among these actions, it needs to activate caffeinate in a separate process and then continue with the rest of the code, allowing the secondary process to run until it is terminated by another command. Unfortunately, this only works when executed from the terminal or the script editor on a Mac and not when launched from the .plist daemon.

When I run:

bash -c "nohup caffeinate -u -i -d &"

from the terminal, the process is created, and I can close the terminal or terminate it with:

killall caffeinate

While I can start and stop it from the script editor as:

var app = Application.currentApplication()
app.includeStandardAdditions = true


var accountUser = "Username";
var myPassword = "Password";

var caffeinateCommand = `bash -c "nohup /usr/bin/caffeinate -u -i -d &"`;
var returnedValue = app.doShellScript(caffeinateCommand, {userName:accountUser, password:myPassword, administratorPrivileges:true});

//
//
// Other Code
//
//

app.doShellScript("killall caffeinate");

When I try to start this same script as a daemon using a .plist file and execute it with launchctl load /path/.plist, the separate process is not created (in fact, instead of returning an empty value, it returns true, and when I run killall caffeinate, it says there are no active processes).

How can I ensure that this process gets started?

NOTE: I've noticed that the error should be in the execution of nohup within the daemon, but I don't know how to replace it. Because when I launch only caffeinate, the process remains on the main one and does not continue with the rest of the code.

0

There are 0 best solutions below