You may find that it would be useful to run a CLI script automatically in the background on a scheduled basis. These instructions provide a way for you to configure your machine to run your script.
There are of course some limitations on your scripts running. These are some considerations, though the full list of limitations will depend on which operating system you are running:
- Your script is subject to the conditions you specify. For example, in Windows, you may choose to not run the script if your user is logged off or if your machine's power cord is not plugged in.
- If your machine is off, your script will not run.
- If your internet connection is disconnected, the script may not be able to perform all of its programmed actions, such as downloading content from remote sources.
In Windows, you can create a Scheduled Task.
- Start by opening the Task Scheduler app by entering it into the Windows search bar. Alternatively, use the Windows Key + R and enter
taskschd.mscinto the Run window.
- Click on Create Task on the right Actions panel.
- In the General tab, give the new task a name and (optional) a description.
- Choose if you want to run the script only if the user is logged on or not.
- Click on the Triggers tab.
- Here is where you can schedule it to run one-time, daily, weekly, or monthly. If you need to run this script multiple times a day, you can use the "Repeat task every..." option to further tune how often the script runs.
- After choosing the time and recurrence interval, make sure the Enabled checkmark is checked below and click OK.
- Now click on the Actions tab.
For the Program/script field, enter the path to PowerShell:
For the Add arguments (optional) field, enter the path to your script:
Click on OK to save your task. Now you've successfully created a Scheduled Task to run your job automatically.
If you are having trouble getting this to work, please try the following:
Make sure the script runs fine through the powershell. Open powershell and then use the cd command to go to the directory that contains your script (e.g. myscript.ps1). Run the script manually by entering:
.\myscript.ps1to see if it runs fine.
If it runs okay, then go to the Task Scheduler and make sure in the Action tab that the Program/script field contains the path to powershell.exe and that the Add arguments section contains a valid path to you script.
If this doesn't work, you can redirect output streams (everything, including warning and error streams) to a file to see if there’s any other info added there. To do this, go back to the Actions tab of the Task Scheduler again. Doublecheck the Program/script points to powershell.exe and then under the Add arguments (optional) section, you can specify both the path to the script and an output file. After the script runs, go to the directory and look for the output log. Look to see if any errors are reported in the log.
On my system for running a script called refresh.ps1 located in the **C:\Users\jeffl\Scripts** folder, I have these settings:
C:\Users\jeffl\Scripts\refresh.ps1 *> C:\Users\jeffl\Scripts\refresh.log
After the task runs, I can go to C:\Users\jeffl\Scripts and open the refresh.log file to see if there were any errors.
- It's possible that the Windows Event Viewer has some more information that could be useful. Open the Event Viewer and then go to the left pane and expand the Applications and Services Logs section. Then click on Windows PowerShell and see if there’s any additional information there.
TBD (cron job)
TBD (cron job)
Updated about 2 years ago