Thank you for your question, it guided me towards this solution. This is my complete, generalized- and particular solution. It consist of 3 steps:
- Create a cronjob
- Run/enable the cronjob service at startup
- Remove prompting for password to start the cronjob service automatically.
In reality your problem is already solved with just step 2 and 3, but since you tried to do it with a cronjob, I also added that step for completeness.
1. Creating a functioning cronjob:
- Browse to folder
/etc/
- Then in folder
/etc/
enter:sudo nano crontab
- In that file named crontab enter your command.
- E.g.:
*/1 * * * * root touch /var/www/myFile
- To create a file named
myFile
in location /var/www/
every minute.
- For completeness:
*/1 * * * * root touch /var/www/myFile
would mean: create that file every 1st minute of the hour.
An example of the crontab
file could look like (I only added the last line, the rest was already there in my setup):
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
*/2 * * * * root touch /var/www/myFile
2. Enabling cronjob service
To run a command automatically at startup of WSL Ubuntu 16.04 you can:
- cd to
/home/<your ubuntu user name>
sudo nano .bashrc
- The text editor nano then creates/opens a file
.bashrc
- 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.
- For example:
echo "hello world"
as shown in the picture below.
- For your particular problem, the particular solution would be to enter the line:
sudo ./xmr-stak-cpu
- Close the editor with:
ctrl+x
- Save the file with
Y
- Exit ubuntu
- 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
For example this could be what your .bashrc
looks like after you edited it: (I only added the first line on top, the rest was already there in my setup.)
sudo -i service cron start
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
# append to the history file, don't overwrite it
shopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color|*-256color) color_prompt=yes;;
esac
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi
You can replace the hello world
command with sudo service cron start
to enable cronjob service. However then you are still required to enter your password manually.
3. Removing prompt for password:
Using: https://askubuntu.com/questions/147241/execute-sudo-without-password
- Open WSL ubuntu 16.04 (terminal)
sudo visudo
- At the bottom of the file add line:
<your WSL ubuntu username> ALL=(ALL) NOPASSWD: ALL
- E.g with username zq you would add the following line to the bottom of the file:
zq ALL=(ALL) NOPASSWD: ALL
- ctrl+x to exit
y
followed by <enter>
to save.
- Then again, close ubuntu and re-open it and verify
- The cron service is running automatically when you boot/open WSL ubuntu 16.04 without prompting for password.
- (you can check with command:
sudo service cron status
.)
The code to prevent prompting for password at boot would for example look like (I only added the last line, the rest was already there in my setup):
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults env_reset
Defaults mail_badpass
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/s$
# Host alias specification
# User alias specification
# Cmnd alias specification
# User privilege specification
root ALL=(ALL:ALL) ALL
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
# See sudoers(5) for more information on "#include" directives:
#includedir /etc/sudoers.d
%sudo ALL=NOPASSWD: /etc/init.d/cron
zq ALL=(ALL) NOPASSWD: ALL
Working towards this solution, I learned cronjobs are intended for things to run periodically rather than at specific events such as startup. To run things at startup in WSL you can use the file /home/<username>/.bashrc
.
1
Possible duplicate of How to run Ubuntu service on Windows (at startup)?
– Biswapriyo – 2018-07-29T06:18:36.9171It is NOT a possible duplicate but an extension of it. The routines explored in that question are way beyond what I need. In addition, this is a specific quation atht also refers to that question. You did not read that question, did you? – seanbw – 2018-07-30T01:18:24.297
1Checking your question, you are running
sudo ... xmr-stak-cpu
using a scheduled task. -- Have you ranvisudo
and included the%sudo ALL=NOPASSWD: /xmr-stak-cpu/bin/xmr-stak-cpu
to avoid problems with the command asking for a password? Do you (really) need the sudo? can you run the miner without sudo privileges? – Jaime – 2018-07-30T15:32:49.527