I have a node script inside /home/pi/Blynk/script.js I've followed various posts online relating using cron and etc/init.d However none seem to be working. The script must be run via sudo, I've checked the log and it shows the command being run, but its not running the script.
Here is what I added to cron
(This cron below runs the Blynk server and works)
#reboot java -jar /home/pi/server-0.27.1.jar -dataFolder /home/pi/Blynk &
(This cron runs the script with user root and node yet doesn't work)
#reboot /usr/bin/sudo -u root -H /usr/bin/node /home/pi/Blynk/script.js
Could someone please show me where I've gone wrong?
Related
Hi im trying to setup a cron job to restart a game server daily but its failing to work
i have used crontab -e to install a job
* 3 * * * systemctl --user restart <servicename>
this failed to restart my process at 3 am as expected.
i also tried moving the command to a bash file and running the bash file through cron (as i have seen alot of people use .sh files to run commands)
additionally i have tried installing to job as root with zero success
this had similar results
my syslog did show a command execute as expected yesterday though the service didnt restart, since then cron isnt showing any new attempts.
i found some info on troubleshooting and following the guidance shows that my cron is working, i managed write the date/time to a file every minute and the service is clearly running and this showed in the logs.
is there a problem with cron running systemctl commands or is there further steps i can use to see why its failing?
Edit:
i tried running my command as root also and nothing happened
Jan 31 09:00:01 ns509515 CRON[108307]: (root) CMD (<user> systemctl --user restart EcoServer)
Jan 31 09:01:01 ns509515 CRON[108330]: (root) CMD (<user> systemctl --user restart EcoServer)
Jan 31 09:02:01 ns509515 CRON[108356]: (root) CMD (<user> systemctl --user restart EcoServer)
Start nodejs app on linux server with ssh(if i close the ssh connection,app stopped why?)
1-create nodejs app -its oke
2-run on linux server -its oke(i stop the apache server)
But if i close the ssh connection(with my windows pc),app stopped.How can i solve this problem?
The most correct thing to do is to write a service file for it so whatever init system you have (likely systemd) will keep it running and manage the start/stop/restart stuff for you.
Failing that (and I don't blame you...) you can run it within the screen utility. Launch it with screen -d -m /path/to/start/script and then you can come back later and reconnect to it with screen -r or screen -r <pid of the screen session>.
Note that launching it that way won't restart it, etc. To do that, you could do something like
#!/bin/sh
while true
do
sleep 3s
/path/to/start/script
done
And call that with the screen command.
Use the nohup command to start the application. Like:
nohup THE_COMMAND_YOU_DONT_WANT_TO_STOP_WHEN_YOU_LOGOUT &
With nodemon it might be helpful to put the command to start the server in a file called myserver.sh containing:
nodemon server.js
Make sure the file is executable:
chmod +x myserver.js
And then run
nohup myserver.sh &
I have a very simple bash script which should launch my ghost blog. I am using crontab to launch the script on startup, here is the crontab command I am running:
#reboot /var/www/ghost/launch.sh
The script has the following code:
#!/bin/sh
ps auxw | grep apache2 | grep -v grep > /dev/null
if [ $? != 0 ]
then
NODE_ENV=production forever start --sourceDir /var/www/ghost index.js
fi
When I sudo reboot the server, and use forever list to find the processes running, I see the following:
data: [0] sHyo /usr/bin/nodejs index.js 1299 1314 /home/webadmin/.forever/sHyo.log 0:0:1:25.957
When I nano to that log file, the log says the following:
^[[31m
ERROR:^[[39m ^[[31mCould not locate a configuration file.^[[39m
^[[37m/home/webadmin^[[39m
^[[32mPlease check your deployment for config.js or config.example.js.^[[39m
Error: Could not locate a configuration file.
at checkTemplate (/var/www/ghost/core/config-loader.js:16:36)
at Object.cb [as oncomplete] (fs.js:168:19)
error: Forever detected script was killed by signal: null
It appears to be looking in /home/webadmin/, but ghost is installed at /var/www/ghost????
When I run the exact same script in the terminal manually after the sever has started up by ssh-ing into the server, the script works fine. I run: cd /var/www/ghost/ and then ./launch.sh and the ghost blog appears and is working fine. The log for that forever process says the following:
^[[32mGhost is running...^[[39m
Your blog is now available on http://blog.example.com ^[[90m
Ctrl+C to shut down^[[39m
What is wrong with my script or crontab that it cannot launch the script properly?
I run: cd /var/www/ghost/ and then ./launch.sh and the ghost blog appears and is working fine.
That's the thing, your cron job is not doing the same:
#reboot /var/www/ghost/launch.sh
This script is executed from your home directory. One way to fix is to change your crontab:
#reboot cd /var/www/ghost; ./launch.sh
Another way is to add this line near the top of launch.sh, anywhere before launching forever:
# change to the directory of this script
cd $(dirname "$0")
Just an FYI for anybody that runs across this I would highly suggest looking into pm2 to start Ghost and to monitor Ghost. It will monitor Ghost like Forever and has a built in feature to generate a init script to start pm2 when your server restarts. Also has better features to monitor Ghost while it is running. Check out my how to here.
I would like to automatically run node server when instances are created (using forever). I am on Ubuntu 11.10 (Canonical), I followed the instructions here exactly on creating launch config using user script: http://alestic.com/2011/11/ec2-schedule-instance
I can't seem to get this to work. Below is my startup script:
#!/bin/bash
set -e -x
/home/MyUserName/node_modules/.bin/forever stopall
/home/MyUserName/node_modules/.bin/forever start node.js/app.js
The launch config is created using this cmd:
as-create-launch-config MyLC --image-id ami-b6a3f8f2 --user-data-file user-data-script.sh --instance-type m1.small
Found the issue, I have to run forever as the user, not root, wonder why...like so:
sudo -u MyUserName /home/MyUserName/node_modules/.bin/forever start node.js/app.js
Are you fully qualifying the app.js file? Could it just be this line?
/home/MyUserName/node_modules/.bin/forever start /home/MyUserName/node.js/app.js
I've been setting up my server recently and today I had to restart it... then I realised all of my Node apps I had running weren't running anymore. I'm using Node Forever module to keep the apps running, but then I realised I still need to have them starting when my server restarts or shut downs and powers up again.
I have been researching the best way to do this, but what I'm trying just doesn't seem to work. I've created an Upstart script in my /etc/init/ folder on my Ubuntu Server 10.04LTS remote server and tried restarting and it doesn't seem to do anything. Nothing is getting listed when I run forever list.
Here is my current Upstart script I was trying out today:
#/etc/init/myapp.conf
start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown
script
exec sudo /usr/local/bin/node /var/www/myapp/myapp.forever.js
end script
I use Forever in a Node script as I find it easier to configure it how I want. It's confirmed that the script runs just fine if I do this outside the script, there is just something wrong with the Upstart script itself. It seems to have the same permissions as all the other Upstart scripts in /etc/init/ folder.
As an additional note, I have gone through almost all the answers I could find here on StackOverflow, and that it how I got together the script that I have at present.
UPDATE:
With Tom's answer, I have now tried:
#/etc/init/myapp.conf
start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown
exec sudo /usr/local/bin/node /var/www/myapp/myapp.forever.js
But it's still not working.
So I don't know why this isn't running when I restart my server. Please help!
This is not a very happy setup. The way upstart works is that it starts your process running it using the process id for the start command. Forever JS works similarly, it is probably inspired by Upstart.
When you try to run forever.js with upstart, the forever process you create in your upstart script exits immediately after starting. Upstart counts on having the process continue to run.
When I tried to run forever using upstart, I wound up with five different forever process running because upstart thought it had failed to start forever, and it retried five times.
Did you try doing it without the start script lines?
description "my server"
author "name"
start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown
#respawn if you were not using forever
exec sudo /usr/local/bin/node myapp.forever.js
Source: http://caolanmcmahon.com/posts/deploying_node_js_with_upstart
I've opted to use an #reboot statement in the user's crontab file, which will execute forever on server restarts.
#reboot forever start app.js
Additional Reading - http://www.cyberciti.biz/faq/linux-execute-cron-job-after-system-reboot/