# how to run commnad on startup the easiest way ?



## isso2095 (Oct 17, 2021)

hi all,

i need to run this commnad on startup:

```
vboxmanage startvm "OpenWrt" --type headless
```

ps i'm new to freebsd   any help would be apperciated

i tried put script in /usr/local/etc/rc.d

which is openwrt.sh and contain thre following without #bin/bash
sleep 45 && vboxmanage start vm "OpenWrt" --type headless


----------



## ralphbsz (Oct 18, 2021)

You are probably starting a service? Where "service" means something that should be running most of the time, should be started in an organized fashion while booting, and shut down while the system comes down? The cleanest solution is to write a service script. In FreeBSD, that's sort of easy: Take one of the existing service scripts in /etc/rc.d or /usr/local/etc/rc.d as an example, and modify it. Note that those scripts are not just a few lines; they have to follow a pretty fixed format. Here is one I used for an equipment monitoring daemon:

```
#!/bin/sh

# PROVIDE: eqmon
# REQUIRE: DAEMON

# Add the following lines to /etc/rc.conf to enable':
# eqmon_enable="YES"

. /etc/rc.subr

name="eqmon"
rcvar=`set_rcvar`

command="/usr/local/sbin/eqmon_daemon"
pidfile="/var/run/$name.pid"

# read configuration and set defaults
load_rc_config "$name"
: ${eqmon_enable="NO"}

if [ $# -gt 0 -a "$1" = "stop" ]
then
  /usr/local/bin/eqctl quit
  exit
fi

run_rc_command "$1"
```

If you don't care about doing it in an organized and neat fashion, and being able to control it: You can put it into /etc/rc.local (which is hacky and may suffer accidents during upgrades), or use a crontab entry with the first few numbers replaced by "@reboot" (but getting things to work right in cron has pitfalls).


----------



## Vull (Oct 18, 2021)

Practical rc.d scripting in BSD
					

A guide to writing new rc.d scripts and understanding those already written




					docs.freebsd.org


----------



## tux2bsd (Oct 18, 2021)

isso2095

/etc/crontab

`@reboot root /root/banana.sh`

Then when you're ready to learn a more proper way take a look at what these guys are talking about.


----------



## sko (Oct 18, 2021)

Or just dump virtualbox and use bhyve with some management tool (like e.g. sysutils/iohyve or sysutils/chyves) that just lets you set "boot=on" for that VM...
This also solves a whole lot of other problems with virutalbox (e.g. not ZFS aware, horribly bloated, tooling/syntax is a nightmare etc. pp...)


----------



## obsigna (Oct 18, 2021)

Given that VirtualBox instances are managed by the command line tool vboxmanage, which is neither a daemon nor a service, IMHO, it would be sort of over-engineering to build an rc script around vboxmanage.

I would put the vboxmanage startvm ... command into /etc/rc.local

And, perhaps, after some thorough testing of vboxmanage controlvm <VM> acpipowerbutton, I would put that command into /etc/rc.shutdown.local. Not all VMs support a regular ACPI shutdown. vboxmanage controlvm <VM> poweroff (i.e. toggling the power switch off) is not what we usually want. Anyway, you want to measure how long shutting down takes, e.g. X seconds, and add sleep X+margin after the vboxmanage ... acpipowerbutton command.


----------



## kpedersen (Oct 18, 2021)

obsigna said:


> Anyway, you want to measure how long shutting down takes, e.g. X seconds, and add sleep X+margin after the vboxmanage ... acpipowerbutton command.


Whilst unlikely with the OP's OpenWrt VM, it can't always be assumed that a VM will take the same time to shutdown each time.

I would recommend send an acpipowerbutton event but then poll vboxmanage for the VM status. Then when no longer running, allow the host to shutdown.

Another option which I personally prefer is to make the VM completely read-only. Then you can treat it like a simple appliance and shut it off freely without any risk. I have a tutorial for OpenBSD here. Similar will be possible for most capable operating systems.


----------

