How to start mongod from boot script without running as root? - linux

mongod runs fine as my own system user, but when I attempt to start it from a boot script using sudo, it fails.
I'm not running it as root, i'm doing:
sudo -u normaluser /user/local/bin/mongod --fork --logpath=/var/log/mongodb.log --logappend >/dev/null 2>&1 &
The log file is writeable by normaluser and I have no problem running it as normaluser directly.
How can I start this on boot?

Ok, given our conversation in the comments above I think you should look at the following file:
Red Hat based init script
It seems like you might want to follow that and write something like:
daemon --user normaluser mongod --fork --logpath=/var/log/mongodb.log --logappend >/dev/null 2>&1 &

Related

how to run process from batch script

I have simple batch script in linux debian - Debian GNU/Linux 6.0 - that stop process then deletes log files and start the process again :
#!/bin/bash
killall -KILL rsyslogd
sleep 5s
rm /var/log/syslog
rm /var/log/messages
rm /var/log/kern.log
sleep 3s
rsyslogd
exit
The process name is rsyslogd. I have to close it before deleting the log files, for linux to empty the space from disk.
I see that killall -KILL closes the process by its name, but what is the opposite - the run command?
Calling it by its name without any command seems to not work. I will be glad for any tips, thank you.
Debian uses systemd to manage processes. You should, therefore, use the systemd's commands to stop and start rsyslogd.
systemctl stop rsyslog
and
systemctl start rsyslog
If you are using really old versions of Debian (so old that you should upgrade), it may be possible that sys V is still used. In that case, there is a file under /etc/init.d which is called rc.rsyslog or something comparable (use ls /etc/init.d to find the exact name). In that case, it would be
sudo /etc/init.d/rc.rsyslog stop
and
sudo /etc/init.d/rc.rsyslog start
Or it may be, that your systemd-package may be broken. In that case, the package can be re-installed:
apt-get --reinstall install systemd
To start rsyslogd:
systemctl start rsyslog
To stop it:
systemctl stop rsyslog
If you want to do both, use
systemctl restart rsyslog

Auto-run python script when system is booted on jetson nano

How to auto-run python script made by me when the system is booted on jetson nano?
Step 1: Create a shell file.
sudo nano /usr/local/bin/startup.sh: Type this on the terminal. A new sh file is created. This file consists of the python file that is to be executed. I gave the name startup.sh. It can be any name XYZ.sh
#! /bin/sh: This is called the shebang. This would execute our script using a Bourne shell. This tells the system that the commands in this file should be fed to the interpreter.
sleep 10: This pauses a script for a certain amount of time. He re we pause it for 10 seconds.
In the next line, we insert the code that we use to run the program on the terminal.
OPENBLAS_CORETYPE=ARMV8 /usr/bin/python3 path/of/the/python/code.py
It looks like this:
#! /bin/sh
sleep 10
OPENBLAS_CORETYPE=ARMV8 /usr/bin/python3 /home/sooraj/Downloads/incabin-monitoring-system-main/netstreamfeb17.py
Now we close the file using Ctrl+X. and save it.
Step 2: Create a service file
sudo nano /etc/systemd/system/startup.service
Things to write inside it.
[Unit]
Description = INTU_IPC start-uo specific script
[Service]
Type= idle
ExecStartPre = /bin/sleep 10
ExecStart = /usr/local/bin/startup.sh
User=sooraj# write your user name here
[Install]
WantedBy = multi-user.target
Now we close the file using Ctrl+X. and save it.
step 3: Give permission.
sudo chmod +x /usr/local/bin/startup.sh
step 4: enable, start and stop
sudo systemctl enable startup.service
sudo systemctl start startup.service
sudo systemctl stop.service
After starting, to view if it works, we can observe it in the terminal by
journalctl -u startup.service -f
If we edit the service file for the next time, we need to reload it before enabling and starting.
sudo systemctl daemon-reload
sudo systemctl enable startup.service
sudo systemctl start startup.service
Additional information.
systemctl is-enabled startup.service #checks if the service file is enabled.
systemctl is-failed startup.service #checks if the service file failed to start.
systemctl is-active startup.service #checks if the service file is active.
sudo systemctl list-unit-files — type=service #display the list of service files.
Try StartupApplications and add your python execution shell command with proper path.
An even better solution will be to use crontab.
crontab -e
Add #reboot python path/to/script so that the script gets executed every time you reboot.
This link might help you.
As an alternative to systemd or crontab, you can try pm2. It's very easy to configure and use. Just follow a quick start guide. Or just test it the following way:
pm2 start app.py
pm2 save
Note that you should initially generate a startup script to make it work on boot.

Linux usermod user is currently used by process

So I'm trying to change user502's directory using
usermod -d /home/user502home user502
When I enter than into the shell (I'm on root user) I get "usermod: user502 is currently used by process 4220" and I know the user isn't logged in or anything, I just made the user.
How do I fix this?
You must kill currently used process first. But this process probably is your SSH connection. So, in this case you need to run this commands on nohup.
sudo su
nohup kill 4220; sleep 2; usermod -d /home/user502home user502 &
After you can connect to SSH again.
I got this error when using WSL ubuntu. I fixed it by creating a temp user, setting WSL to launch via that user (ubuntu config --default-user username), logging in as that user, and using usermod there.
An improvement on the top answer: you can use fuser to kill processes which are using your home directory without having to track down the pid.
sudo su
nohup fuser -k /home/USERHERE; sleep 2; usermod -d /home/USERHERE USERHERE &
After some research, this process can be an reoccurring error in Ubuntu and you only need to restart the system and it will go away.
Log in as root, then open terminal and run
kill -9 -u [old_username] && username -l [old_username] [new_username]
After that run
adduser [new_username] sudo
to be able to run sudo commands with the new username.

Running forever within su as a different user

I am experiencing weird behavior with forever, which I want to use to keep alive my node app.
I want to run my forever processes as my regular user lwood, not as root.
I need to know how to run forever properly within root mode, but as the user lwood. (This is needed because, for example as a special case, upstart scripts run as root.)
These commands illustrate my problem (I'm on Ubuntu 12.04, and $ is regular user and # is root):
$ su
[type in su password]
# cd /home/lwood/myapp
# sudo -u lwood forever -a -l "/home/lwood/myapp/logfile.log" start app.js
info: Forever processing file: app.js
# forever list
info: No forever processes running
# exit
$ forever list
info: No forever processes running
So forever successfully started, yet no processes are running under neither lwood nor root!
How can I fix this problem?
If you're using upstart, try this (putting it to your upstart script)
exec su -s /bin/sh -c 'exec "$0" "$#"' username -- /usr/local/bin/forever ...
reference: https://superuser.com/questions/213416/running-upstart-jobs-as-unprivileged-users
On systems with systemd (RHEL 7, CentOS 7, Debian, Ubuntu, Fedora, ...) you can use this script as /usr/lib/systemd/system/nodejs.service or equivalent (check for other *.service files):
[Unit]
Description=Node.js Application
After=postgresql.service network.target
Wants=postgresql.service
[Service]
ExecStart=/usr/bin/su - <user> -c '/usr/bin/npm start --prefix /path/to/app'
WorkingDirectory=/path/to/app
Restart=always
RestartSec=30
[Install]
WantedBy=multi-user.target
Probably because it's running as a different context on a different user. Maybe you need to add sudo a swell for the list:
sudo -u lwood forever list

Upstart error terminated with status 1

I have an ubuntu 10.04 server and tried to create an upstart script:
description "node-workerListener"
author "me"
start on startup
stop on shutdown
script
# We found $HOME is needed. Without it, we ran into problems
export HOME="/var/www"
exec sudo -u www-data /usr/local/bin/node /var/www/vhost/node/test/workerListener.js 2>&1 >> /var/log/node/helloworld.log
end script
This should start a node script, which works, if I start it manually on the command line. But when i try to "start node-workerListener" I get the message "node-workerListener start/running, process 1323", but it doesn't.
In /var/log/syslog: "...init: node-workerListener main process (1317) terminated with status 1"
What can I do?
You can also use forever https://github.com/nodejitsu/forever to run the node process.
Here is a detailled article : http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever

Resources