Ubuntu 8.04 Hardy and node.js upstart script - javascript

I am trying to write an upstart script for my ubuntu machine, which is version 8.04 "Hardy". I have followed the instructions on this site: upstart for node.js but it seems like these instructions are for a current version of ubuntu.
I noticed that the /etc/init directory does not exist on my machine, first I tried putting the script in the /etc/init.d directory and then I created the /etc/init dir and placed it there.
I will post my upstart script below (which is basically the same as from the website above with some path changes), but when I run start jobname, I just get an error "start: Unknown job: jobname". So then I changed the script around to a slimmed down version, posted below, and still I get the same result.
For now, I am using the 'nohup' command to run my node server but I would like a more permanent solution.
Please, any help?
SCRIPT 1:
description "node.js chat server"
author "iandev ith3"
# used to be: start on startup
# until we found some mounts weren't ready yet while booting:
start on started mountall
stop on shutdown
# Automatically Respawn:
respawn
respawn limit 99 5
script
# Not sure why $HOME is needed, but we found that it is:
export HOME="/root"
exec /root/local/node/bin/node /home/ian/chat.js >> /var/log/node.log 2>&1
end script
post-start script
# optionally put a script here that will notifiy you node has (re)started
# /root/bin/hoptoad.sh "node.js has started!"
end script
SCRIPT 2:
description "node.js chat server"
author "iandev ith3"
script
exec /root/local/node/bin/node /home/ian/chat.js >> /var/log/node.log 2>&1
end script

Just use Forever. https://github.com/indexzero/forever

From looking at the website you provided I'd say that the /etc/init was just a typo and it should be /etc/init.d/. Some things you may want to check:
executable flag on your scripts. With most versions of Ubuntu executable files show up green when running 'ls' from the command line. If you want to check if your file is executable run 'ls -l /etc/init.d/YOUR_SCRIPT' from the command line. You will see something like this:
-rwxr-xr-x 1 root root 1342 2010-09-16 10:13 YOUR_SCRIPT
The x's mean that it is executable.
To set the executable flag if it is not set, run chmod u+x YOUR_SCRIPT
I'm pretty sure for older versions of ubuntu you need to have the script in /etc/rc.d/rc3.d or /etc/rc3.d. What linux does is run through rc0.d to rc5.d and execute every script in there. From what it looks like, ubuntu is moving away from this to something simpler so if you have rc directories you may need to edit your script a little.
Anyway I think i'm getting a little over complicated here. Check your executable flag and if you have rc directories and we'll move on from there.

May not be the best thing to start a process with sudo, but here's what I have setup on my local pc:
#!upstart
description "node.js server"
author "alessio"
start on startup
stop on shutdown
script
export HOME="/ubuntu"
exec sudo -u ubuntu /usr/bin/node /home/ubuntu/www/test.js 2>&1 >> /var/log/node.log
end script
Hope this helps.

Related

How to restart a Node.js project?

I'm not a Node.js developer. So I have no idea how it works. I've been a PHP developer for over 8 years.
Because of some reason, I need to make a small change in a Node.js project which is live. All I have to do is changing a payment gateway token. I did it like this:
After pulling it on the server, users still go to the old payment gateway. So I guess I need to do a restart. (I'm saying so because, for PHP projects, when you change a config-related thing, you need to restart PHP).
Not sure should I restart what thing? Noted that, the server is Ubuntu 20.04 and uses Nginx to talk to Node.js. In other word, how can I see Node is running as what service on Linux?
Also, there are two files that I think I need to run the project again after restarting Node through one of them: index.js, server.js. Am I right?
And
Your Node.js script likely runs under a process that restarts the script in case it dies. There are several "run forever" wrappers, the most popular one is pm2. Find out which one is used in your project. Try pm2 list as the user your project executes under. If pm2 type pm2 restart app_name to restart your project.
Please check if it is a node.js project so you can write the command node index.js or node server.js with this command you can start your node server.

start node and pm2 application from bat file

I have a chat-bot application running on node and I keep it always active thanks to pm 2.
I would like to improve the way I launch the application. Instead of running the start command from the console, it would be nice to double click a .bat file.
I am trying to develop the bat file, but I lack knowledge.
I am grateful for any help.
#echo off
SET PM2_HOME=C:\Users\Usuario\.pm2
pm2 start C:\Users\Usuario\Desktop\ROBOTs\Chatbot SCTR\app.js
echo servicio ejecutado
This bat file that I developed does not work. I know that I am not calling the variables, because I don't know how to include it, since I always execute the pm2 start app.js command.
my application does not use ports like 8080 and others, because the same library allows me to establish a connection and with pm 2 I keep it always active.
add the start command to your package.json for launching your app with pm2, then with your bat file just direct it to run with npm or yarn, whatever your default package manager is
edit:
here is a sample of a script in bash, but the concept will be the same for batch
#!/bin/bash
## detect operating system machine so we can setup some environment variables
UNAME="$(uname -s)"
case "${UNAME}" in
Linux*) OS='linux';;
Darwin*) OS='mac';;
CYGWIN*) OS='cygwin';;
MINGW*) OS='mingw';;
*) OS="UNKNOWN:${UNAME}"
esac
## if OS is Ubuntu (IE Production Box) set the path of variables
if [ $OS == 'linux' ]
then
YARN=/usr/bin/yarn
PM2=/usr/bin/pm2
fi
## if OS is Mac (IE Development Box) set the path of variables
if [ $OS == 'mac' ]
then
YARN=/usr/local/bin/yarn
PM2=/usr/local/bin/pm2
fi
## run the app
cd /var/www/application || exit
$YARN run productionStart
$PM2 save
exit $?
here is the line of code for starting the app on mac/linux we use from our package.json
"productionStart": "pm2 start ecosystem.config.js --env=production",
for more information about starting your app with an ecosystem file with pm2, see the docs here

How can I let multiple Java scripts to run concurrently in Frida?

I want to bypass root detection, certificate pinning, and a crc integrity check for an android app using Frida. I can't run more than 1 script at once, is there any solution?
Concatenate all scripts into one file and load is the trivial (and probably good enough) answer.
I have another suggestion that you can do even without a computer, just put text editor on your device and get a bluetooth keyboard.
frida-inject which can be downloaded from frida release page
Push frida-inject & the scripts to the device and execute the following that iterates scripts , inject them to the process and exists.
for f in $(ls /sdcard/scripts/*js) do; ./frida-inject -p $(pidof com.app.name) -s $f --eternalize
frida-inject options
frida -U -f [APP_ID] -l script1.js -l script2.js
you can try this too

Bot shuts down when putty window is closed

I created a discord bot and am now attempting to run it off an Ubuntu Machine.
I installed the folders of the bot and NodeJs, here is what I used to install NodeJS:
sudo apt-get install -y nodejs
Then I used cd to select the directory, and started my bot using node index.js
The bot started, however when I went to close the putty and keep it running on the VPS the bot shutdown. Here is what the directory looks like.
I think the problem is that when you start the app in the putty window, that process is linked to the window and gets terminated when that is closed.
To avoid that you can use a host service like screen, tmux, nohup, bg and so on...
If you want to know which is the best, try looking at this question from the askUbuntu Stack Exchange.
The key concept is that you open a new window using the tmux command (or screen, ...), then run your bot like you always do. When you want to leave but keep the process runing, you can detach the session with a key combination, that changes from service to service.
If you want to access that window again, you can run a command that will "restore" your session, like
tmux list-sessions
tmux attach-session -t 0
The NodeJS instance is terminated when putty is closed. You need something to keep the instance alive. Try:
PM2: http://pm2.keymetrics.io/
or,
Forever: https://github.com/foreverjs/forever#readme
Recommended though is to run the node instance as a service that can reboot on startup. Try looking at this:
https://stackoverflow.com/a/29042953/7739392
The shell runs in the foreground. This means any scripts you start there will end once you end your session. A simple solution would be to run your script in the background by adding the & after the call:
node index.js &
A better solution would be to create a service you can ask the service daemon to run for you. However, adding the & should get you what you want for now.
I recommend using one of these two node modules - ForeverJS or PM2. I'll show you how to quickly get started with ForeverJS but PM2 would be very similar.
You can easily install ForeverJS by typing the following in your terminal:
$ npm install forever -g
You may need to use SUDO depending on your user's privileges to get this working properly. It is NOT recommended to use it in production due to the security risks.
Once installed CD to your projects file directory and like you typed 'node index.js' you will do something similar with ForeverJS.
$ forever start index.js
Now when you exit the terminal your NodeJS application will remain as a running process.

Including js from raw.github.com

I have a github.com demo page that is linking to https://raw.github.com/.../master/.../file.js so that I don't need to always copy the .js file over to the gh-pages branch every time it's changed. This works in every browser except IE which complains:
SEC7112: Script from https://raw.github.com/cwolves/jQuery-iMask/master/dist/jquery-imask-min.js was blocked due to mime type mismatch
This complaint is coming from the fact that the file is transferred with:
X-Content-Type-Options: nosniff
Content-Type: text/plain
which I can't change.
Anyone have any ideas how to accomplish this same thing? Somehow allowing me to link to the file in the master branch without having to always push it to the gh-pages branch?
Actual page: http://cwolves.github.com/jQuery-iMask/
(Minor update -- I changed the gh-pages in this exact instance to include the .js file, so IE is no longer broken, but would still like any feedback :))
You can try using https://rawgit.com/ service.
Just replace raw.github.com with rawgit.com
UPDATE
The Rawgit service (former Rawgithub) has been shutdown.
RawGit has reached the end of its useful life
October 8, 2018
GitHub repositories that served content through RawGit within the last month will continue to be served until at least October of 2019. URLs for other repositories are no longer being served.
If you're currently using RawGit, please stop using it as soon as you can.
I can't help you with tricking IE, and I think from that angle what you are looking for is impossible (and discouraged, since that is not the purpose of Github's raw URLs).
However, you can automate committing the changes to gh-pages and pushing to make your life easier. You can do it with a post-commit hook to update the relevant files in the gh-pages branch automatically. I've cooked up such a post-commit script that watches for changes to certain files and commits them to another branch:
#!/bin/sh
WATCH_BRANCH="master"
WATCH_FILES="jquery-imask-min.js"
DEST_BRANCH="gh-pages"
# bail out if this commit wasn't made in the watched branch
THIS_BRANCH=$(git branch --no-color | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/');
if [ "$THIS_BRANCH" != "$WATCH_BRANCH" ]; then
exit 0
fi
# only update if watched files have changed in the latest commit
CHANGED_FILES=$(git show --pretty="format:" --name-only $WATCH_BRANCH)
if $(echo "$CHANGED_FILES" | grep "^$WATCH_FILES$" -q); then
# checkout destination branch, then
# checkout latest version of each watched file and add to index
git checkout -q $DEST_BRANCH
git pull -q
SAVEIFS=$IFS
IFS=$(echo -n "|")
for file in $WATCH_FILES; do
git checkout $WATCH_BRANCH -- $file
git add $file > /dev/null
done
IFS=$SAVEIFS
# commit with a chance to edit the message, then go back to watched branch
LATEST_COMMIT=$(git rev-parse $WATCH_BRANCH)
git commit -m "Also including changes from $WATCH_BRANCH's $LATEST_COMMIT"
git push origin $DEST_BRANCH
git checkout -q $WATCH_BRANCH
fi
Note that this is a general script, though I have specified the config vars at the top for your purposes. $WATCH_FILES can be set to a list of files delimited by braces | such as index.html|js/jquery.js. Paths must be specified relative to the root of the repo.
Let me know if you have any questions, and if the script helps you!
Take a look at raw.githack.com. The idea of this service is inspired from rawgit.com. I just realized that using a whole framework (node.js + express.js) for such simple thing as requests proxying is overkilling, and made same stuff using nginx only.
Replace "githubusercontent" domain name chunk in your github/gist URL with "githack" and you're done!
Furthermore, it supports bitbucket.com - simply replace whole bitbucket domain with bb.githack.com.
Github's raw URLs aren't designed to be a generic web host. Push that stuff off to proper host, like say pages.github.com.
Nowadays theres jsDelivr, its open source, free and fast.
And supports GitHub! https://www.jsdelivr.com/
Furthermore its from trusted people/company.
I say this because I'm not sure we can trust https://raw.githack.com/
I am also trying to achieve this. However, I cannot seem to get the solution from #shelhamer to work in my codebase. Below is the updated post-commit hook script that I used to get it working:
#!/bin/sh
WATCH_BRANCH="master"
WATCH_FILES="jquery-imask-min.js"
DEST_BRANCH="gh-pages"
# bail out if this commit wasn't made in the watched branch
THIS_BRANCH=$(git branch --no-color | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/');
if [ "$THIS_BRANCH" != "$WATCH_BRANCH" ]; then
exit 0
fi
# only update if watched files have changed in the latest commit
CHANGED_FILES=$(git show --pretty="format:" --name-only $WATCH_BRANCH)
if $(echo "$CHANGED_FILES" | grep "^$WATCH_FILES$" -q); then
# checkout destination branch, then
# checkout latest version of each watched file and add to index
git checkout -q $DEST_BRANCH
git pull -q
SAVEIFS=$IFS
IFS=$(echo -n "|")
for file in $WATCH_FILES; do
git checkout $WATCH_BRANCH -- $file
git add $file > /dev/null
done
IFS=$SAVEIFS
# commit with a chance to edit the message, then go back to watched branch
LATEST_COMMIT=$(git rev-parse $WATCH_BRANCH)
git commit -m "Also including changes from $WATCH_BRANCH's $LATEST_COMMIT"
git push origin $DEST_BRANCH
git checkout -q $WATCH_BRANCH
fi
I had to update the use of grep to make the regex successfully match (-P was not an option on the grep implementation included in Git Bash shell for Windows), add a git pull and a git push origin $DEST_BRANCH. Oh, and I had to add an empty shell of the directories and files in advance (perhaps just the directories would have sufficed?).
Since this is actually doing a push, I think it may be better to switch this script to being a post-receive script instead of post-commit. Otherwise, you could be pushing code to gh-pages that never made it into master [if you choose not to push it].

Categories