Run Your CLI Scripts Automatically

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.

🚧

Limitations

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:

  1. 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.
  2. If your machine is off, your script will not run.
  3. 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.

WINDOWS 10

Setup

In Windows, you can create a Scheduled Task.

  1. Start by opening the Task Scheduler app by entering it into the Windows search bar. Alternatively, use the Windows Key + R and enter taskschd.msc into the Run window.
  2. Click on Create Task on the right Actions panel.
Create a new task.Create a new task.

Create a new task.

  1. In the General tab, give the new task a name and (optional) a description.
  2. Choose if you want to run the script only if the user is logged on or not.
  3. Click on the Triggers tab.
Give the task a trigger, such as a time or date and recurrence.Give the task a trigger, such as a time or date and recurrence.

Give the task a trigger, such as a time or date and recurrence.

  1. 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.
  2. After choosing the time and recurrence interval, make sure the Enabled checkmark is checked below and click OK.
  3. Now click on the Actions tab.
Enter the path to powershell for the Program/script, then add your powershell script to the Add arguments field.Enter the path to powershell for the Program/script, then add your powershell script to the Add arguments field.

Enter the path to powershell for the Program/script, then add your powershell script to the Add arguments field.

For the Program/script field, enter the path to PowerShell:
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe

For the Add arguments (optional) field, enter the path to your script:
C:\Users\<REPLACE_WITH_YOUR_USER_NAME>\Scripts\odrive_replicate.ps1 (example)

Click on OK to save your task. Now you've successfully created a Scheduled Task to run your job automatically.

Troubleshooting

If you are having trouble getting this to work, please try the following:

  1. 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.ps1 to see if it runs fine.

  2. 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.

  3. 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.

📘

Example

On my system for running a script called refresh.ps1 located in the **C:\Users\jeffl\Scripts** folder, I have these settings:

Program/Script:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Add arguments:
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.

  1. 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.

If you still need help, you can always leave us a note in the forum or send us an email at [email protected].

MAC

TBD (cron job)

LINUX

TBD (cron job)


Did this page help you?