0

After setting up a cron job, I am trying to: automatically enable cron service when I open the Windows Subsystem for Linux Ubuntu 16.04 app from the Windows App Store.

Currently I am trying to reach that goal by simulating:

  1. Open WSL Ubuntu
  2. enter sudo service cron start
  3. <enter>

To do that, I located the ubuntu1604.exe, made myself owner and gave myself permission to read write and open the ubuntu1604.exe.

Next, I openend cmd in administrator mode, browsed to: C:\Program Files\WindowsApps\CanonicalGroupLimited.Ubuntu16.04onWindows_1604.2018.510.0_x64__79rhkp1fndgsc and entered: ubuntu1604.exe to verify I was able to open WSL ubuntu from cmd without being prompted anything. I was, since it just opened Ubuntu in the same cmd window (visible thanks to the green <user>@<pcname>:~$ displayed in the cmd).

Next I tried to pass an argument to the WSL Ubuntu whilst opening it with:

  1. ubuntu1604.exe run echo "hello world"
  2. ubuntu1604.exe echo "hello world"
  3. ubuntu1604.exe -c echo "hello world"
  4. ubuntu1604.exe -c

However:

  1. Returns hello world but does not open WSL ubuntu.
  2. Returns a form of documentation on how to Launch a Linux distribution (attached in picture below).
  3. Returns hello world but does not open WSL ubuntu.
  4. Opens Ubuntu in the location of the current cmd path, but does not execute

Documentation received in command 2.

So then I tried to open it from a batch script or powershell script with:

  1. Batch script test1.bat:

start /d "C:\Program Files\WindowsApps\CanonicalGroupLimited.Ubuntu16.04onWindows_1604.2018.510.0_x64__79rhkp1fndgsc" ubuntu1604.exe echo "hello world"

  1. Batch script: test2.bat

    Process.Start("C:\Program Files\WindowsApps\CanonicalGroupLimited.Ubuntu16.04onWindows_1604.2018.510.0_x64__79rhkp1fndgsc\ubuntu1604.exe", "echo "hello world"");

  2. Powershell script: test3.ps1

Process.Start("C:\Program Files\WindowsApps\CanonicalGroupLimited.Ubuntu16.04onWindows_1604.2018.510.0_x64__79rhkp1fndgsc\ubuntu1604.exe", "echo "hello world"");

  1. Powershell script: test4.ps1 Process.Start("C:\Program Files\WindowsApps\CanonicalGroupLimited.Ubuntu16.04onWindows_1604.2018.510.0_x64__79rhkp1fndgsc\ubuntu1604.exe", echo "hello world");`

Though:

  1. Opens a big black screen and closes in a flicker.
  2. Process.Start is not recognized as an internal or external command
  3. Is not in powershell language
  4. Is not in powershell language.

Now I am aware perhaps the option to pass any argumen that can execute a command without user interaction upon startup of the WSL ubuntu1604 might be disabled since it is an app of the app store (or for any other reason).

Hence:

  1. If anyone knows whether my syntax is correct but my intention
    will not be possible this way, please let me know.
  2. If it is possible but I am doing it wrong, I would appreciate any feedback on what I am doing wrong, or a solution that does it right.
1

1 Answer 1

1

I think cronjobs are intended to mainly run periodically.

To run a command automatically at startup of WSL Ubuntu 16.04 you can:

  1. cd to /home/<your ubuntu user name>
  2. sudo nano .bashrc
  3. The text editor nano then creates/opens a file .bashrc
  4. In that file a lot of examples can be shown already, to just execute your command upon startup of the WSL ubuntu 16.04, write your command on the first line of the .bashrc file.
  5. For example:echo "hello world" as shown in the picture below.
  6. Close the editor with: ctrl+x
  7. Save the file with Y
  8. Exit ubuntu
  9. Restart ubuntu and verify indeed the hello world is printed before your username.

![An example command in .bashrc that is executed upon boot of WSL ubuntu.]1

You can replace the hello worldcommand with sudo service cron start to enable cronjob service. However then you are still required to enter your password manually. To solve this, using: https://askubuntu.com/questions/147241/execute-sudo-without-password

  1. Open WSL ubuntu 16.04 (terminal)
  2. sudo visudo
  3. At the bottom of the file add line: ` ALL=(ALL) NOPASSWD: ALL
  4. E.g with username zq you would add the following line to the bottom of the file:
  5. zq ALL=(ALL) NOPASSWD: ALL
  6. ctrl+x to exit
  7. y followed by <enter> to save.
  8. Then again, close ubuntu and re-open it and verify
  9. The cron service is running automatically when you boot/open WSL ubuntu 16.04 without prompting for password.
  10. (you can check with command: sudo service cron status.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.