1

I have a below command which I have to run manually each time whenever the Ubuntu machine starts or reboots.

ngrok start --all

This command can only be run without sudo. If I run it as sudo ngrok start --all, it gives error. Due to this I am not able to create script which I can run as systemd service, so that the service automatically starts on every boot and executes the script and the command starts.

I have also tried crontab by adding below line to sudo crontab -e but it also didn't worked.

@reboot ngrok start --all

How can I run above command without sudo on startup of Ubuntu. Can anyone please help. Thanks

8
  • Just use crontab -e without the sudo?
    – muru
    May 10, 2021 at 16:23
  • @muru Let me try it now.
    – S Andrew
    May 10, 2021 at 16:25
  • @muru its not working with crontab -e without sudo
    – S Andrew
    May 10, 2021 at 16:30
  • @muru When I run ngrok start --all, it loads up my portal. But using it in crontab, portal is not loading.
    – S Andrew
    May 10, 2021 at 16:36
  • @muru It starts my website www.mywebsite.com. I can open it in my browser.
    – S Andrew
    May 10, 2021 at 16:44

2 Answers 2

1

At that time, it already runs with root privileges, so there is no need to use sudo. Just run it without it.

You only need sudo when you are logged in, and a program runs using your user account with less privileges; sudo gives such a program root privileges. Processes started during system startup or in (root's) crontab already run as root.

There is even a solution how to run this as a systemd unit:

https://github.com/vincenthsu/systemd-ngrok

9
  • I didn't get you, can you explain a bit more
    – S Andrew
    May 10, 2021 at 16:25
  • So you mean, we can not run commands without sudo at startup.? There is no solution for it or am I missing something?
    – S Andrew
    May 10, 2021 at 16:30
  • You are thinking too complicated. sudo runs a program as root. At boot time EVERYTHING already runs as root. Just put that plain command into a systemd unit; there is no need for sudo.
    – HuHa
    May 10, 2021 at 16:31
  • I don't think it will run as systemd service. but let me try it once more.
    – S Andrew
    May 10, 2021 at 16:32
  • 1
    Using old crontab is not going one step further, it's putting more skeletons into that old musty closet. Use today's technology, i.e. systemd!
    – HuHa
    May 10, 2021 at 17:05
0

Using @reboot in your crontab is the way to go, but there is something you should know about jobs run through cron:

Jobs run through cron, or at, or batch, aren't run in the same runtime environment that you have on your desktop. None of your PATH changes, or other environment variable settings are automatically propagated to your cron job. For example, there's no $DISPLAY, so GUI programs need special treatment (read man xhost).

One can set environment variables for all one's cron jobs in the crontab file Read man 5 crontab.

Look at the results of echo "=== set ===";set;echo "=== env ===";env | sort;echo "=== alias ===";alias in each of your environments.

Since the command part of the crontab line is, by default, interpreted by /bin/sh, which has a simpler syntax than /bin/bash, I recommend having command be a call to a bash script (executable, mounted, starts with #!/bin/bash) which sets up the environment, then calls the desired program.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .