2

I wrote a bash script to create a scheduled task and run a django app. I am using Git Bash as my terminal, and I have been able to manually run the commands in the snippet posted below with success. However, when I run bash script with these same commands, the scheduled task and the django app are never run. Why is there a discrepancy in behavior and how can I correct this in my bash script?

#!/usr/bin/env bash

// Create scheduled task
echo schtasks //create //tn my-task //tr '"python app/manage.py loaddata /resources/output.json"' //sc daily //st 09:30 //ri 60 //et 16:00

// Run app
echo python app/manage.py runserver

echo "TERMINATED"

$SHELL
Arny
  • 23
  • 3
  • I'm not 100% sure, but I'm guessing that Windows scheduled tasks fire up the command shell rather than Git Bash. Perhaps try creating the virtualenv in Windows and running the command as you would from `cmd.exe` rather than Git Bash? – FlipperPA Dec 03 '18 at 03:15
  • @FlipperPA I can create the scheduled tasks on Git Bash when I manually input the command though. The issue is only when I include it in my bash script. Unless you want me to run the script in `cmd.exe`? But it would still need to use something like Git Bash or Cygwin to execute the script. – Arny Dec 03 '18 at 04:20

1 Answers1

0

For a bash script to run from a CMD session (triggered by the Windows scheduler), you would need:

  • a script named git-xxx (replace xxx by the name of your chosing)
  • that script in your Windows PATH (as well as Git itself)
  • a schedule task running git xxx (note the space)

That would run git-xxx in a Git bash session.

The other option would be, still from a CMD (or a scheduled task) to run:

bash -c "/c/path/to/your/script"

In both instances, make sure bash is not the one from WSL if you are on Windows 10, and have activated that feature.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I simply want to run a bash script that triggers a scheduled task on my Windows machine. Using CMD isn't a requirement. It seems like there should be a way to do this via Git Bash or Cygwin, since I've been able to run the scheduled tasks commands manually and use different bash scripts in Git Bash. Can you advise? – Arny Dec 05 '18 at 06:26
  • @Arny "using CMD is implied when your scheduled job will call `git xxx`. Did you try and setup a scheduled job calling that command? A `git xxx` script will in turn runs in a git bash session. – VonC Dec 05 '18 at 07:11
  • No, I have not tried that, but I don't see how it achieves my goal. I only want the scheduled task to run the python command `python app/manage.py loaddata /resources/output.json`. It does not matter if this command is run in a git bash session or a CMD session. – Arny Dec 06 '18 at 06:52
  • And I want this scheduled task executed from the bash script for convenience, so I only have to run `source .sh` to execute everything. – Arny Dec 06 '18 at 06:53
  • @Arny to get the equivalent of `source .sh`, please see my answer: use `bash -c "/c/path/to/your/script"`. – VonC Dec 06 '18 at 07:22