Multiline write fails (Permission denied) - linux

I would like to write a multiline string to a file that is only accessible with root permissions.
This is what I have so far:
sudo cat > /etc/init/myservice.conf <<EOL
start on runlevel [2345]
stop on runlevel [06]
respawn
script
cd ~/somefolder
. venv/bin/activate
exec python manage.py runserver 0.0.0.0:8080
end script
EOL
When this command, all I get is "Permission Denied". Creating the file manually works.

The output redirection is processed before sudo runs, so you are still trying to open the file as yourself, not as root. One option is to use tee instead of cat, since tee, rather than the current shell, will open the file for writing.
sudo tee /etc/init/myservice.conf > /dev/null <<EOF
start on runlevel [2345]
stop on runlevel [06]
respawn
script
cd ~/somefolder
. venv/bin/activate
exec python manage.py runserver 0.0.0.0:8080
end script
EOF
While the delimiter used doesn't really matter, it makes more sense to use something that indicates end of file, rather than end of line :) tee actually writes its input to both the named file(s) and standard output, but you don't need to see what you are writing, hence the redirection to /dev/null.

Related

Inconsistent stderr output between shell and systemd

I have a little script running at startup:
At first i was using crontab with #reboot tag, but, since some time i need to modify the script, i ended up looking at systemd for an easy restart of the script
my crontab config was:
#reboot sleep 10; cd /path/to/the/script/ && nohup ./script.py >> stdout.txt 2>> stderr.txt
and everything was working fine
I'm now using systemd with this config:
[Unit]
Description=Script
[Service]
WorkingDirectory=/path/to/the/script/
ExecStart=/path/to/the/script/script.py
StandardOutput=file:/path/to/the/script/stdout.txt
StandardError=file:/path/to/the/script/stderr.txt <----- Problem here
User=my_user
[Install]
WantedBy=graphical.target
the problem is:
before stderr was correctoly written to stderr.txt
now both stdout and stderr are written to stdout.txt, even if i specified a different file for stderr
There are also no errors looking at
journalctl -e -u python-coolosseo
BTW i tried both file: and append:

what is the best way to start a script in boot time on linux

I want to start a script I have on when the system start and looking for the best way, my way is:
vi /etc/systemd/system/myscript.service
[Service]
Type=simple
ExecStart=/usr/bin/myscript
CPUSchedulingPolicy=rr
CPUSchedulingPrioty=27
[Install]
WantedBy=multi-user.target graphical.target
systemctl daemon-reload; systemctl enable myscript; systemctl start rmyscript
it's working good but just wondered if there another and better way.
There are a couple of ways to achieve this, but you will need root privileges for any the following. To get root, open a terminal and run the command:
sudo su
and the command prompt will change to '#' indicating that the terminal session has root privileges.
Alternative #1. Add an initscript
Create a new script in /etc/init.d/myscript:
vi /etc/init.d/myscript
(Obviously it doesn't have to be called "myscript".) In this script, do whatever you want to do. Perhaps just run the script you mentioned:
#!/bin/sh
/path/to/my/script.sh
Make it executable:
chmod ugo+x /etc/init.d/myscript
Configure the init system to run this script at startup:
update-rc.d myscript defaults
Alternative #2. Add commands to /etc/rc.local
vi /etc/rc.local
with content like the following:
# This script is executed at the end of each multiuser runlevel
/path/to/my/script.sh || exit 1 # Added by me
exit 0
Alternative #3. Add an Upstart job
Create /etc/init/myjob.conf:
vi /etc/init/myjob.conf
with the following content:
description "my job"
start on startup
task
exec /path/to/my/script.sh
BTW:
You don't need to be root if you can edit your crontab (crontab -e) and create an entry like this:
#reboot /path/to/script.sh
This way, you can run it as a regular user. #reboot just means it's run when the computer starts up (not necessarily just when it's rebooted).

Cron Job Killing and Restarting Python Script

I set up a cron job on a linux server to kill and restart a python script (run.py) every other day. I set the job to run as root, but I find that sometimes it doesn't kill the process properly (and ends up running two scripts in a row).
Is there a better way to do this?
My cron job parameters:
0 8 * * 1,4,7 cd /home/myUser && ./start.sh
start.sh:
#!/bin/bash
echo "Running..."
sudo pkill -f run.py
sudo python run.py &
I guess run.py runs as python, not run.py. So you won't find anything with kill -f run.py.
You should echo the PID of the process to a file and use that value to kill the previous process if it's still running. Just add echo $! >/path/to/pid.file as the last line in your start.sh script to do so.
Read more:
https://serverfault.com/questions/205498/how-to-get-pid-of-just-started-process
How to read a file into a variable in shell?
http://www.cyberciti.biz/faq/kill-process-in-linux-or-terminate-a-process-in-unix-or-linux-systems/
Example to get you started:
#!/bin/bash
echo "Running..."
sudo pkill -F /path/to/pid.pid
sudo python /path/to/run.py &
echo $! > /path/to/pid.pid
Another alternative to this is making the python script run on upstart if you are on a system that supports upstart. Then you can just do sudo /sbin/start job_name at the begin and sudo /sbin/stop job_name this makes upstart manage the pids for you.
Python upstart script
Upstart python script

UPSTART script non root not working

I'm trying to run a nodejs application using upstart as a non root user.
But somehow parts of the script will not run : for instance:
if I run it like a root user(below example) NODE_ENV never gets called/set
the only way to called is with "sudo initctl stop pdcapp"
sudo nameofApp start|stop would not work
When called sudo initctl stop nameofApp the pre-stop script will not echo to the log file
if I try to runit like a non root user it would not even start
isn't a more cleaner easier way of doing this (systemd) I've looked a various tutorials around and apparently this is how they've doneit. so what am I missing here?
This is the .conf file under /etc/init/
env FULL_PATH="/srv/pd/sept011100/dev"
env NODE_PATH="/usr/local/nodeJS/bin/node"
env NODE_ENV=production
start on filesystem or runlevel [2345]
stop on [!2345]
script
export NODE_ENV #this variable is never set
echo $$ > /var/run/PD.pid
cd $FULL_PATH
# the command below will not work
#exec sudo -u nginx "$NODE_PATH server.js >> /var/log/PD/pdapp.log 2>&1"
exec $NODE_PATH server.js >> /var/log/PD/pdapp.log 2>&1
end script
pre-start script
echo "[`date`] (sys) Starting" >> /var/log/PD/pdapp.log
end script
pre-stop script
rm /var/run/pdapp.pid
echo "[`date`] (sys) Stopping" >> /var/log/PDC/pdapp.log
end script
in /var/log/messages I get this when I stop the application, otherwise I get nothing in the logfile
Sep 2 18:23:14 547610-redhat-dev2 init: pdcapp pre-stop process (6903) terminated with status 1
Sep 2 18:23:14 547610-redhat-dev2 init: pdcapp main process (6899) terminated with status 143
any Ideas why is this not working I'm running redhat 6.5
Red Hat has a super old version of Upstart that is probably full of bugs because they never contributed to Upstart, despite using it (Fedora switched to systemd right after RHEL 6 was released, before they even really tried it out well).

Forever, node: Strange upstart behavior (restart behaving same as stop)

This is my upstart file /etc/init/myapp.conf on my Ubuntu 12.04.
description "Example of starting Node with Upstart and Forever"
start on filesystem or runlevel [2345]
stop on runlevel [06]
expect fork
console output
setuid lwood
env HOME=/home/lwood
script
cd $HOME
exec forever -a -l /home/lwood/myapp/applog.log \
start /home/lwood/myapp/app.js
end script
pre-stop script
cd $HOME
exec forever stop /home/lwood/myapp/app.js
end script
Do you know why $ sudo restart myapp is only stopping the service? It does not start the service after stopping it. So it has the same effect as $ sudo stop myapp.
Doing $ sudo stop myapp and then $ sudo start myapp works fine though. Wondering why restarting behaves different.
Can you output anything? Output your environment, and specifically your path variable and see if they are different from when you can start it, to when it fails.
When it fails, is there an output file and what is the error?
My guess is that the path is not yet fully set up.

Resources