We are trying to start a go program which runs localhost on the specified port when the system reboots using shell and crontab. Following is the work we have done so far:
- created a shell file called goinit.sh. In which we have written
#!/bin/sh echo "go program started at" `date` >> /Users/nicolas/goinit1.txt cd /Users/nicolas/go/src/goinit go build exec goinit -port 2000
- We have created the executable file for the goinit.sh using commands
sudo chmod u+x goinit.sh
- Then we started the script manually. Using
sudo ./goinit.sh
Until here everything works fine. when we open the browser and start my localhost:2000 my program work as expected.
- We have used crontab so as to run our script file when my system reboots. Following are the lines written in crontab.
@reboot /Users/nicolas/goinit.sh
When we reboot the system script runs but nothing is visible in the browser. It simply shows file not found when we open our browser and run localhost:2000.
We have done a bit of research on the file not being served on reboot.
Why doesn't “cd” work in a shell script?
As we are new to shell scripting the concept of sub shell is bit confusing. We have tried numerous examples to make this shell script work but none of then really helped us.
Note : All of this is done in mac OS.