Using a batch file to start pm2 when windows starts? - windows

I can't get pm2 to start my apps on Windows start. I'm running Windows Server 2012 R2 Standard and pm2 2.4.2.
I have a pm2 process file in JSON format which I use to start all my apps.
c:\pm2\process.json
{
"apps": [
{
"name" : "my-app",
"script" : "c:\\node\\myapp\index.js"
}
]
}
I have a batch file which uses the JSON file:
c:\pm2\pm2-startup.bat
#echo off
set HOMEDRIVE=C:
set PM2_HOME=C:\etc\.pm2
setx /M PM2_HOME C:\etc\.pm2
cd C:\pm2 & pm2 start process.json
I have a Windows task scheduled to run the batch file:
Trigger: At startup
Run under: An administrator account
Run whether user is logged in or not: Yes
Run with highest privileges: Yes
Action: Start a program
Script: C:\pm2\pm2-startup.bat
Start in: C:\pm2
If I run the batch file manually (double clicking it), it works. If I run the scheduled task manually (right-click, run), it works.
When I restart the server and check the scheduled task, it has run, no errors, however the apps are not running. Doing pm2 list shows no apps in the table.
I don't want to use pm2-windows-service because I don't want to run pm2 as a service (tried it and it was flaky).
I don't want to use pm2-windows-startup either as it doesn't seem to work with a pm2 process file, it just tries to remember what was running before.
I do want to use a plain batch file on startup.
What am I doing wrong with the batch file..? Why does the scheduled task run ok, but the pm2 list is empty..?

I know this is an old question, but I'll leave an answer for future searches.
You can put a shortcut to the .bat file in the Windows Startup folder.
Assuming the PM2 cmd is in path, your bat file could look like this.
#echo off
SET PM2_HOME=C:\Users\pstart\.pm2
pm2 start C:\path\to\run.js
That'll start PM2 and run the program. Now you just take the bat file and save it somewhere and create a shortcut in the Windows Startup folder by clicking the Start button -> Run -> shell:startup and pasting it in there.

Related

running shell script with windows task scheduler

I currenty have a simple shell script that I created for a linux machine to be run using cron, but now I want to be able to run the file using windows task scheduler. I have tried to get it to work using cron for cygwin, but even after running cron-config successfully and ensuring that the shell script can be executed successfully, for some reason the cron task simply wasn't executing. So I decided to give in and use the windows task scheduler. In order to do this, I looked at the following posts about the issue:
Cgywin .sh file run as Windows Task Scheduler
http://www.davidjnice.com/cygwin_scheduled_tasks.html
in my case, the entry in the "actions" tab of the new task looks like this:
program/script: c:\cygwin64\bin\bash.exe
arguments: -l -c "/cygdrive/c/users/paul/bitcoinbot/download_all_data.sh >> cygdrive/c/users/paul/bitcoinbot/logfile.log 2>&1"
start in: c:\cygwin64\bin
Notice that I redirected the output of the shell script to a log file, so that I should be able to see there whether the program run. Other than that, I simply edited the "trigger" tab to run the task daily, and set the time to a couple of minutes in the fture to see whether it ran successfully.
Alas, when I look at the detailed event history for the task, nothing changes when the trigger time passes. And when I manually "run" the task, the event history seems to add a few different events, but the task is completed within seconds, whereas this task should take over an hour (and it does when the shell script is executed directly from the terminal). And when I look for the log file that should have been created, there is nothing.
Does anyone have any idea what might be the issue here? How can I get my task to run properly at the trigger time, and how can I make sure it does so?
Best,
Paul
EDIT:
here are the pictures showing event history, as per Ken White's request.
Please ignore the fact that it says there are 24 events. These are from multiple separate runs of the task. The events shown here are a complete list of the events triggered by a single run.
EDIT 2:
Regarding my attempts to get cron to work, I have run into the following problem when I try to start the cron service using cygrunsrv. First of all, I tried to start cron by typing
cygrunsrv -I cron -p /usr/sbin/cron.exe -a -D
Now when I type
$cygrunsrv -Q cron
Service: cron
Current State: stopped
Command: /usr/bin/cron.exe
Now, I tried to start the cron service by typing
cygrunsrv -S cron
Cygrunsrv: Error starting a service: QueryServiceStatus: Win32 error 1062:
The service has not been started.
Does anyone hae any idea what this error means? I tried googling it, but couldn't find any answers.

How to run task scheduler in windows 10 with Laravel

I just want to ask how to use it's task scheduling feature on windows machine (my local machine).
I've read it's documentation and I've notice that it's using a Cron.
Any help is truly appreciated.
To run Laravel Scheduler in Windows 10 you need:
Create batch file, like this one and save it:
cd c:\laravel-project\
c:\php5\php.exe artisan schedule:run 1>> NUL 2>&1
Go to Windows 10 Task Scheduler (fast way is press Win+R and enter taskschd.msc).
Click Create basic task, choose When I logon trigger and then choose Start a program -> your .bat file.
Check Open properties dialog option and click Finish.
In task properties click Triggers, then click New and add new trigger Repeat task every - 1 minute.
Now this task will run Laravel scheduler every one minute.
I still did not run the schedule, the solution was just to add / d to the path in
cd c:\laravel-project\
in
cd /d c:\laravel-project\

Task Sheduler: How to run batch file through cmd instead of taskeng.exe?

I wrote a simple script:
cd C:\TESTS\example
call git pull
cd C:\TESTS\example\AutoApp\bin\debug
start AutoApp.exe
I created daily task in sheduler and when task running it open taskeng.exe. That "command line" do not have any git/cmd command wich I use in script. And my application and git pull do not work.
If I open batch file by click on it, it works fine(git pull done and app run done) and run through cmd.
After firs option of anwser task is running all time.
After second option of anwser.
How to solve this problem?
Change the command line used in Task Scheduler to call cmd.exe to launch the batch file instead:
cmd /c "YourBatchFile.bat"
Or
%comspec% /c "YourBatchFile.bat"

Calling cscript.exe and passing a vbscript to call

I have scheduled a task in windows server 2008R2 ..I want to run a VBScript so when I setup the task I call the cscript at C:\Windows\System32\cscript.exe and in the arguments section I am passing //nologo //B d:\main\programs\copy.vbs /targets:contents but it is not executing my script ..If I call my script directly in the start program section it works fine but it's not working if I call CSrcipt and pass in arguments the status changes to queued but nothing happens after that..Can someone tell me what I am doing wrong here.
Also another question I have is that can we run 2 programs one after another in one task like when one script is finished I would like to start another script .
Thanks
Put this line at the top of your script and try again:
CreateObject("WScript.Shell").LogEvent 4, "Script running"
Unless you get an Information event with source WSH and event-ID 4, your script isn't running at all. Check the eventlog and the task's History tab for clues as to why that is. Also check the permissions of the script. Is the runas account of the task able to access/run the file? You can check that by starting a CMD instance as that user
runas /user:DOM\USER cmd
and then trying to run the script in that CMD instance.
Also double-check the task settings. As which user is it configured to run? With the user logged on or not logged on? Is "Run with highest privileges" enabled (in case UAC is enabled on your server)?
If the script does produce the abovementioned event that means it's running in principle, but something is going wrong in the process. You need to debug your script.

Running remotely Linux script from Windows and get execution result code

I have the current scenario to deal with:
I have to schedule the backup of my company's Linux-based server (under Suse Linux) with ARCServe R15 (installed on Windows 2003R2SP2).
I know I have the ability in my backup software (ARCServe) to add pre/post execution scripts to my backup-jobs.
If failure of the script, ARCServe would be specified NOT to run the backup-job, and if success, specified to be run. I have no problem with this.
The problem is, I want to make a windows script (to be launched by ARCServe) for executing a Linux script on the cluster:
- If this Linux-script fails, I want my windows-script to fail, so my backup job in ARCServe wouldn't run
- If the Linux-script success, I want my windows-script to end normally with error code 0, so my ARCServe job would run normally.
I've tried creating this batch file (let's call it HPC.bat):
echo ON
start /wait "C:\Program Files\PUTTY\plink.exe" -v -l root -i "C:\IST\admin\scripts\HPC\pri.ppk" [cluster_name] /appli/admin/backup_admin
exit %errorlevel%
If I manually launch this .bat by double-clicking on it, or launching it in a command prompt under Windows, it executes normally and then ends.
If I make it being launched by ARCServe, the script seems never to end.
My job stays in "waiting" status, it seems the execution code of the linux script isn't returned to my batch file, and this one doesn't close.
In my mind, what's happening is plink just opens the connection to the Linux, send the sript execution signal, and then close the connection, so the execution code can't be returned to the batch. Am I right ?
Is what I want to do possible or am I trying something impossible to do ?
So, do I have to proceed differently ?
Do I have to use PUTTY or CygWin instead of plink ?
Please, it's giving me headaches ...
If you install Cygwin, you could do it exactly like you can do it on Linux to Linux, i.e. remotely run a command with ssh someuser#remoteserver.com somecommand
This command will return with the same return code on the calling client, as the command exited with on the remote end. If you use SSH shared keys for authentication instead of passwords, it can also be scripted without user interaction.

Resources