How to write a script for delete all the logs everytime macos shutdown? - bash

I knew how to delete all the log files with terminal command "sudo rm -r /var/log" but how to set it up for system events, therefore, clear up everytime system shutdown?

We can execute commands or script at startup or shutdown,
see the links below for details,
unix.stackexchange.com or ccm or opensource.com

Related

Auto Start Script

So I am making a script that can run these commands whenever a server boot/reboot:
sudo bash
su - erp
cd frappe-bench/
bench start >/tmp/bench_log &
I found guides here and there about how can I change user in script I came out with the following script:
#! /bin/sh
sudo -u erp bash
cd /home/erp/frappe-bench/
bench start >/tmp/bench_log &
And, I have created a service at /etc/systemd/system/ and set it to run automatically when the server boots up.
The problem is, whenever I run sudo systemctl start erpnextd.service and checked the status, it came up with this
May 24 17:10:05 appbsystem2 systemd[1]: Started ERPNext | Auto Restart.
May 24 17:10:05 appbsystem2 sudo[18814]: root : TTY=unknown ; PWD=/ ; USER=>erp ; COMMAND=/bin/bash
May 24 17:10:05 appbsystem2 systemd[1]: erpnextd.service: Succeeded.
But it still doesn't start up ERPNext.
All I wanted to do is make a script that will start erpnext automatically everytime a server reboot.
Note: I only install frappe-bench on user erp only
Because you are using systemd, you already have all the features from your script available, and better. So you don't even need the script anymore:
[Unit]
Description=...
[Service]
# Run as user erp.
User=erp
# You probably also want to run as group erp, if it exists.
Group=erp
# Change to this directory before executing.
WorkingDirectory=/home/erp/frappe-bench
# Redirect standard output to the given log file.
StandardOutput=file:/tmp/bench_log
# Redirect standard error to the same log file.
StandardError=file:/tmp/bench_log
# Command line for starting the program. Make sure to use an absolute path!
ExecStart=/full/path/to/bench start
[Install]
WantedBy=multi-user.target
Using crontab (the script will start after every reboot/startup)
#crontab -e
#reboot sh /full/path/to/bench start >/tmp/bench_log
The answer provide by Thomas is very helpful.
However, I found another workaround by adding the path of my script file into the bottom of /etc/rc.local file.
Both method works, just a matter of preference ;)

Run shutdown command inside bash script

I'm trying to make an executable file (bash script) to show me a notification and shutdown my computer when a process is not found.
I will run the script as a Startup Application and I'm using the notify-send and shutdown commands in this script.
The problem is:
(1) If I add myfolder/myscript to the Startup Applications list it can't run the shutdown command (root password is required for this)
(2) If I add the script sudo myfolder/myscript it can't show the notifications via notify-send application.
I've already done a lot of searching around the internet and tried these steps:
(1) Added the script path or /sbin/shutdown to the sudores via sudo visudo
(2) Added su - $USER -c "DISPLAY=$DISPLAY DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$UID/bus before notify-send command (I found some users reporting that root can't send notifications)`
So... none of them worked. What I'm missing?
What can be done to display notifications AND shutdown?
Here is my code:
#!/bin/bash
#Search for a specific process and sleep if it is found (removed for space saving)
shut_time=$(date --date='10 minutes' +"%T")
notify-send -t 600000 "WARNING:
Program is not running.
Shutting down in 10 minutes (scheduled for $shut_time)."
#ALREADY TESTED BELLOW LINES (DON'T WORK)
#su - $USER -c "DISPLAY=$DISPLAY DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$UID/bus notify-send -t 600000 'WARNING:
#Program is not running.
#Shutting down in 10 minutes.'"
sudo /sbin/shutdown -h +10 #Tried with our without sudo
I'm running MX Linux 18 (xfce, Debian based).
To execute a terminal or any commands even another bash script within a BASH SCRIPT, all you have to do is simply start with a dollar sign and enclose the whole commands and arguments if any with PARENTHESES as follows.
$(COMMANDS)
In his case, it would be
$(sudo shutdown 10)
The statement above will EXECUTE the SHUTDOWN command for 10 minutes system shutdown and spit out the actual date and time the system will be automatically shutdown just like you would run this command in a console. There is no need to turn the user into sudoer or superuser. Whenever he runs his bash script as a ROOT USER or using SUDO, he will be PROMPTED to enter the root password. That's all he has to do and the above command will be executed.
Plus, if there is ever a need to capture the output of any command or script, do as follow.
my_shuttime=$(sudo shutdown 10)
I think it lacks an entry for shutdown in the sudoers. Please create a file sudo under /etc/sudoers.d and make the following entry:
[YOURUSER] ALL = (ALL) NOPASSWD: /sbin/shutdown
Replace [YOURUSER] with your user account!

Run an shell script on startup (not login) on Ubuntu 14.04

I have a build server. I'm using the Azure Build Agent script. It's a shell script that will run continuously while the server is up. Problem is that I cannot seem to get it to run on startup. I've tried /etc/init.d and /etc/rc.local and the agent is not being run. Nothing concerning the build agent in the boot logs.
For /etc/init.d I created the script agent.sh which contains:
#!/bin/bash
sh ~/agent/run.sh
Gave it the proper permissions chmod 755 agent.shand moved it to /etc/init.d.
and for /etc/rc.local, I just appended the following
sh ~/agent/run.sh &
before exit 0.
What am I doing wrong?
EDIT: added examples.
EDIT 2: Just noticed that the init.d README says that shell scripts need to start with #!/bin/sh and not #!/bin/bash. Also used absolute path, but no change.
FINAL EDIT: As #ewrammer suggested, I used cron and it worked. crontab -e and then #reboot /home/user/agent/run.sh.
It is hard to see what is wrong if you are not posting what you have done, but why not add it as a cron job with #reboot as pattern? Then cron will run the script every time the computer starts.
Just in case, using a supervisor could be a good idea, In Ubuntu 14 you don't have systemd but you can choose from others https://en.wikipedia.org/wiki/Process_supervision.
If using immortal, after installing it, you just need to create a run.yml file in /etc/immortal with something like:
cmd: /path/to/command
log:
file: /var/log/command.log
This will start your script/command on every start, besides ensuring your script/app is always up and running.

Start/Assign macOS app with root privileges

I have written a macOS Cocoa app using XCode 9.2.
The purpose of the app is as a wrapper to a set of shell commands so that users can run commands easily as required. The shell script allow the users to control a particular device driver for which the software is written by another party.
I have managed to get the app to invoke the shell command as root.
But there are some sub-commands that fail. The actual commands run are, and I think it is the 2nd "stop -a" that fails:
do shell script \"
cd '/Library/Application Support/ABC' ;
python '/Library/Application Support/ABC/abc.py' stop -d;
python '/Library/Application Support/ABC/abc.py' stop -a;
python '/Library/Application Support/ABC/abc.py' stop -g;
python '/Library/Application Support/ABC/abc.py' stop -t;\"
with administrator privileges
When I run the same commands in Terminal they work, either as discrete commands or in a shell script.
As a last resort I ran the app from the command line using
sudo ~/Library/Developer/Xcode/DerivedData/appname/Build/Products/Debug/appname.app/Contents/MacOS/appname
And this works perfectly.
So, it seems that when I run using the NSAppleScript framework, which correctly prompts me for the root password, there is still something amiss which causes the command failure. But when the whole app runs under sudo everything works.
So the solution appears to be to set the app to require Root privs. Is there a setting that can be set for the built project that would automatically set this such that the root password is requested on startup?
Seems to be working in that I do not get the error, but would still like some pointers on how to set an app to run as admin.

Editing crontab using a script

I am trying to write an embedded application upgrade script. I am running my application on Ubuntu on Beaglebone Black.
Right not, I am launching my application on power-up. To do this, I am running launch.sh script in crontab.
This script is as mentioned below:
until /root/aa_main; do
echo "Application aa_main crashed with exit code $?. Respawning.." >&2
echo "Crashed! $(date)" >> crashlog.txt
sudo sync
sudo reboot
done
It basically, reboots the system if my application crashes, and crontab launches the application again on reboot.
The problem is that if I want to update the application. I have to perform following steps:
Disable launching of the above script with crontab -e
Reboot
Copy and replace aa_main
Enable launching of the same script with crontab -e
Reboot
I want to automate this process. How can I do it?
Is it possible to edit crontab using a script?
Thanks in advance
You can use crontab command's other options to copy the crontab to a file, modify it and install the modify version from a script:
crontab -l would list the current crontab file, you can redirect this to a file of your choice and modify it
crontab <filename> would install the file specified by <filename> as the new crontab - use this option with your modified file and you'll get the new modified crontab

Resources