I have created a new cronjob to execute my GoLang app upon my RaspberryPi starting up.
To do this I've opened my crontab by running crontab -e and added the following line to it:
#reboot /home/pi/dev/my-app/main
I've run crontab -l and can confirm that it is there.
However when I restart my RaspberryPi nothing happens.
If I manually cd into /home/pi/dev/my-app and run ./main in the terminal the app executes.
What is wrong with my crontab line? How can I execute my GoLang app on startup?
Related
I have a discord bot running on a raspberry pi that i need to restart every day, I'm trying to do this through crontab but with no luck.
It manages to kill the active screen processes but never starts an instance, not that I can see with "screen -ls" and I can tell that it doesn't create one that I can't see as the bot itself does not come online.
Here is the script:
#!/bin/bash
sudo pkill screen
sleep 2
screen -dmS discordBot
sleep 2
screen -S "discordBot" -X stuff "node discordBot/NEWNEWNEWN\n"
Here is also the crontab:
0 0 * * * /bin/bash /home/admin/discordBot/script.sh
Is it possible to have crontab run a screen session? And if so how?
Previously I tried putting the screen command stright into cron but now I have it in a bash script instead.
If I run the script in the terminal it works perfectly, it’s just cron where it fails. Also replacing "screen" with the full path "/usr/bin/screen" does not change anything.
So the best way of doing it, without investigating the underlying problem would be to create a systemd service and putting a restart command into cron.
/etc/systemd/system/mydiscordbot.service:
[Unit]
Description=A very simple discord bot
[Service]
Type=simple
ExecStart=node /path/to/my/discordBot.js
[Install]
WantedBy=multi-user.target
Now you can run your bot with systemctl start mydiscordbot and can view your logs with journalctl --follow -u mydiscord bot
Now you only need to put
45 2 * * * systemctl restart discordbot
into root's crontab and you should be good to go.
You probably should also write the logs into a logfile, maybe /var/log/mydiscordbot.log, but how and if you do that is up to you.
OLD/WRONG ANSWER:
cron is run with a minimal environment, and depending on your os, /usr/bin/ is probably not in the $PATH var. screen is mostlikely located at /usr/bin/screen, so cron can't run it because it can't find the screen binary. try replacing screen in your script with /usr/bin/screen
But the other question here is: Why does your discord bot need to be restarted every day. I run multiple bots with 100k+ users, and they don't need to be restarted at all. Maybe you should open a new question with the error and some code snippets.
I don't know what os your rpi is running, but I had a similar issue earlier today trying to get vms on a server to launch a terminal and run a python script with crontab. I found a very easily solution to automatically restart it on crashes, and to run something simply in the background. Don't rely on crontab or an init service. If your rpi as an x server running, or anything that can be launched on session start, there is a simple shell solution. Make a bash script like
while :; do
/my/script/to/keep/running/here -foo -bar -baz >> /var/log/myscriptlog 2>&1
done
and then you would start it on .xprofile or some similar startup operation like
/path/to/launcher.sh &
to have it run the background. It will restart automatically everytime it closes if started in something like .xprofile, .xinitrc, or anything ran at startup.
Then maybe make a cronjob to restart the raspberry pi or whatever system is running the script but this script wil restart the service whenever it's closed anyway. Maybe you can put that cronjob on a script but I don't think it would launch the GUI.
Some other things you can do to launch a GUI terminal in my research with cronjob that you can try, though they didn't work for my situation on these custom linux vms, and that I read was a security risk to do this from a cronjob, is you can specify the display.
* * * * * DISPLAY=:0 /your/gui/stuff/here
but you would would need to make sure the user has the right permissions and it's considered an unsafe hack to even do this as far as I have read.
for my issue, I had to launch a terminal that stayed open, and then changed to the directory of a python script and ran the script, so that the local files in directory would be called in the python script. here is a rough example of the "launcher.sh" I called from the startup method this strange linux distro used lol.
#!/bin/sh
while :; do
/usr/bin/urxvt -e /bin/bash -c "cd /path/to/project && python loader.py"
done
Also check this source for process management
https://mywiki.wooledge.org/ProcessManagement
I am trying to run a start.sh file when I start my AWS EC2 instance. Currently I have to start the instance connect to it create a screen and start the start.sh file within it and disconnect.
I have tried putting the below code within the User data, which should run on start-up however when I connect to see if its working I can't see evidence of any screens active or the start.sh file running. I have looked in the /var/log/cloud-init.log but cannot see evidence of it trying to run (though I do not know what to look for)
#!/bin/bash
sudo su
cd
screen
sudo ./start.sh
The start.sh file is located in the root users default directory
The easiest way is using a cron job for this. Add your script to the cron file with:
crontab -e
add:
#reboot sh ./start.sh
with the #reboot your job will be executed after startup.
The program should run on boot. the nano operating system (jet-pack) not allowing the auto-login too. I tried to put the script into its startup file but the program doesn't boot.
Run you script in cron job.
sudo crontab -e
* * * /usr/bin/python my_script.py
This will make your script run on boot.
Kindly provide complete paths in script to run error-free
I am trying to get my PI, which is running rasbian, to automatically run a bash command whenever the pi is booted. The ultimate goal is to open the localhost web page.
I ran
crontab -e
to open my crontab, and added this to the end of the file:
#reboot chromium-browser --kiosk localhost
but when I reboot the pi, nothing appears to happen. I then added this to test to see if crontab was even running or if I had formatting wrong:
#reboot cd && sudo touch sucess
But it executed exactly as expected
What am I missing?
EDIT 1: I did try doing this but it still didn't execute:
#reboot cd && chromium-browser --kiosk localhost
EDIT 2: I also saw an article suggesting that cron cannot find bash command locations, so I tried this as well, also to no avail:
#reboot cd && /bin/chromium-browser --kiosk localhost
EDIT 3: I also attempted to put the same command in root's crontab instead by doing sudo crontab -eBut that didn't work either
I want to run this command on my ubuntu server installation on startup:
~/factorio/bin/x64/factorio --start-server-load-latest --server-settings ~/factorio/data/server-settings.json
I tried putting this code in the /etc/rc.local directory:
~/factorio/bin/x64/factorio --start-server-load-latest --server-settings ~/factorio/data/server-settings.json || exit 1
exit 0
And the command doesn't run on startup. I know that the command runs when I type it into the command line, but it won't run for some reason when I put it in the /etc/rc.local file. Can anyone tell me where the error is in my code or another way that I could get this command to run on boot?
Open crontab.
sudo crontab -e
Then try adding you command as root to the bottom.(use nano editor).
#reboot root ~/factorio/bin/x64/factorio --start-server-load-latest --server-settings ~/factorio/data/server-settings.json
Note that this will run under root so you will need to change the path of every file from '~/call' to '/full/path/to/file'