I have developed an application time tracking app in python for linux, I am using xprop to get the active window, then it's PID , window name and process name. This app sends the tracking data to a remote server and so with all the dependencies i make a linux package using pyinstaller.
I'm currently working in CentOS 7. I have written a service which will keep the application open ( i made all the required changes for the service to run in user's desktop environment ).
while testing the service I saw that after a reboot with a fresh desktop, the service is running fine, but the data is not getting collected on the server. But as soon as i open a terminal everything works fine.
Is it necessary to have a terminal open for subprocess.check_output / subprocess.Popen() if the code is as follows :
active_win = subprocess.check_output(["xprop", "-root", "_NET_ACTIVE_WINDOW"]).decode('utf-8').split('#')[1].strip()
or
root = Popen( ['xprop', '-root', '_NET_ACTIVE_WINDOW'], stdout = PIPE )
stdout, stderr = root.communicate()
m = re.search( b'^_NET_ACTIVE_WINDOW.* ([\w]+)$', stdout )
if m is not None:
window_id = m.group( 1 )
I am running my node.js application exe file on windows 7/10 startup. It is working fine but some cases my exe stops working and is closed automatically. I want to create some scheduled job for checking whether my exe is running or not. If not running start the application and if running do nothing.
Please guide me to create this kind of schedule job with 10 minutes.
#ECHO OFF
tasklist | find /i "app.exe" && echo process Already running || START "app.exe" "path"
create batch file using above code and use task scheduler for monitoring the exe is running
Can any node.js experts tell me how I might configure node JS to autostart a server when my machine boots?
I'm on Windows
This isn't something to configure in node.js at all, this is purely OS responsibility (Windows in your case). The most reliable way to achieve this is through a Windows Service.
There's this super easy module that installs a node script as a windows service, it's called node-windows (npm, github, documentation). I've used before and worked like a charm.
var Service = require('node-windows').Service;
// Create a new service object
var svc = new Service({
name:'Hello World',
description: 'The nodejs.org example web server.',
script: 'C:\\path\\to\\helloworld.js'
});
// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
svc.start();
});
svc.install();
p.s.
I found the thing so useful that I built an even easier to use wrapper around it (npm, github).
Installing it:
npm install -g qckwinsvc
Installing your service:
> qckwinsvc
prompt: Service name: [name for your service]
prompt: Service description: [description for it]
prompt: Node script path: [path of your node script]
Service installed
Uninstalling your service:
> qckwinsvc --uninstall
prompt: Service name: [name of your service]
prompt: Node script path: [path of your node script]
Service stopped
Service uninstalled
If you are using Linux, macOS or Windows pm2 is your friend. It's a process manager that handle clusters very well.
You install it:
npm install -g pm2
Start a cluster of, for example, 3 processes:
pm2 start app.js -i 3
And make pm2 starts them at boot:
pm2 startup
It has an API, an even a monitor interface:
Go to github and read the instructions. It's easy to use and very handy. Best thing ever since forever.
If I'm not wrong, you can start your application using command line and thus also using a batch file. In that case it is not a very hard task to start it with Windows login.
You just create a batch file with the following content:
node C:\myapp.js
and save it with .bat extention. Here myapp.js is your app, which in this example is located in C: drive (spcify the path).
Now you can just throw the batch file in your startup folder which is located at C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
Just open it using %appdata% in run dailog box and locate to >Roaming>Microsoft>Windows>Start Menu>Programs>Startup
The batch file will be executed at login time and start your node application from cmd.
This can easily be done manually with the Windows Task Scheduler.
First, install forever.
Then, create a batch file that contains the following:
cd C:\path\to\project\root
call C:\Users\Username\AppData\Roaming\npm\forever.cmd start server.js
exit 0
Lastly, create a scheduled task that runs when you log on. This task should call the batch file.
I would recommend installing your node.js app as a Windows service, and then set the service to run at startup. That should make it a bit easier to control the startup action by using the Windows Services snapin rather than having to add or remove batch files in the Startup folder.
Another service-related question in Stackoverflow provided a couple of (apprently) really good options. Check out How to install node.js as a Windows Service. node-windows looks really promising to me. As an aside, I used similar tools for Java apps that needed to run as services. It made my life a whole lot easier. Hope this helps.
you should try this
npm forever
https://www.npmjs.com/package/forever
Use pm2 to start and run your nodejs processes on windows.
Be sure to read this github discussion of how to set up task scheduler to start pm2: https://github.com/Unitech/pm2/issues/1079
Here is another solution I wrote in C# to auto startup native node server or pm2 server on Windows.
I know there are multiple ways to achieve this as per solutions shared above. I haven't tried all of them but some third party services lack clarity around what are all tasks being run in the background. I have achieved this through a powershell script similar to the one mentioned as windows batch file. I have scheduled it using Windows Tasks Scheduler to run every minute. This has been quite efficient and transparent so far. The advantage I have here is that I am checking the process explicitly before starting it again. This wouldn't cause much overhead to the CPU on the server. Also you don't have to explicitly place the file into the startup folders.
function CheckNodeService ()
{
$node = Get-Process node -ErrorAction SilentlyContinue
if($node)
{
echo 'Node Running'
}
else
{
echo 'Node not Running'
Start-Process "C:\Program Files\nodejs\node.exe" -ArgumentList "app.js" -WorkingDirectory "E:\MyApplication"
echo 'Node started'
}
}
CheckNodeService
Simply use this, install, run and save current process list
https://www.npmjs.com/package/pm2-windows-startup
By my exp., after restart server, need to logon, in order to trigger the auto startup.
Need to create a batch file inside project folder.
Write this code in batch file
#echo off
start npm start
save batch file with myprojectname.bat
Go to run command and press window + R
Enter this command :- shell:common startup
Press ok then folder will be open.
Folder path like as C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp
You will be paste your myprojectname.bat file.
You can check also. Need to system restart.
Copied directly from this answer:
You could write a script in any language you want to automate this (even using nodejs) and then just install a shortcut to that script in the user's %appdata%\Microsoft\Windows\Start Menu\Programs\Startup folder
I have a node.js script that plays a mp3 file with mpg321 triggered by HTTP request on my RasPi 3B and want to run continuously even after rebooting the Pi.
I'm able to play a mp3 file as a background job with forever start command, and also able to run a simple script that does not involve mp3 after rebooting with crontab setting. However, although everything is working fine, the mp3 sound is always missing only when I reboot.
Does anyone know a way to get around this issue?
Node.js script:
var mpg321 = require('mpg321');
var filepath = "./audio/beep-01a.mp3";
var player = mpg321().remote();
//infinity loop
player.play(filepath);
player.on('end', function () {
console.log('end');
player.play(filepath);
});
Crontab settings:
#reboot /usr/bin/forever start /home/pi/Documents/nodejs/index.js
I found the cause which is interesting.
A relative file path doesn't work when you run it after rebooting although it works perfectly when you run the script explicitly typing a command from the terminal window by yourself. So every path used in your script needs to be an absolute path.
Hope it helps someone who runs into the same problem in the future.
There is a linux script that contain a statement used to run a java application.
Script (runServer.sh) is like:
java ServerApp &
Since java application is a server , it keeps running forever until gets stopped. Therefore after running runServer.sh it does not return console automatically and keeps waiting to press return key.
And same problem couses remote script call via Runtime api waiting forever.
proc = rt.exec(runScript);
exitVal = proc.waitFor();
Even When running remote script via ssh say from machine1, crtl+c has to be used to exit from remote script execution.
When I insert following statement into runServer.sh, problem is resolved. But in that case I could not write process id into a file via "echo $? >pid"
exec > "\tmp\outlog.txt" 2>&1
Is there a way of returning console automatically by modifiying linux script.
Change the script to:
nohup java ServerApp &