How do I turn off bluetooth via terminal/shell/automator/applescript on a Mac? It should be pretty easy.
BTW, I know you can get applescript to press the bluetooth menu and then press turn bluetooth off. I don't want this if possible.
I'm going to use blueutil.
-- gadgetmo
There are two ways to go about it. You can tell launchd to unload the Bluetooth daemon and no longer start it on demand, or you can programmatically toggle the preference for it and stop the server.
For the former method, use launchctl to tell launchd to unload the daemon and set its disabled flag:
# launchctl unload -w /System/Library/LaunchDaemons/com.apple.blued.plist
If you want to restore it later, this should suffice:
# launchctl load -wF /System/Library/LaunchDaemons/com.apple.blued.plist
That should do it. Now for the latter method, first update the preference file (same thing that would happen when toggling from the UI):
# defaults write /Library/Preferences/com.apple.Bluetooth.plist ControllerPowerState -BOOL 0
Then, you can just rudely kill off the server:
# killall blued
Later, you can restore the preference by resetting the bit:
# defaults write /Library/Preferences/com.apple.Bluetooth.plist ControllerPowerState -BOOL 1
Then kick launchd to have it fire up blued again:
# launchctl start com.apple.blued
Related
On macOs Catalina have problems with making screenshots over cron.
When manually run do_screenshot.sh script then all fine.
But when it run auto over cron - probs, only menu correct, instead window content shown macOs background(see pic)
do_screenshot.sh:
#!/bin/bash
DATEFULL=`date '+%Y%m%d%H%M%S'`
FILENAME="/Users/yak/Documents/screenshots/"$DATEFULL.png
/usr/sbin/screencapture -x $FILENAME
Ran into this problem after the update too. Spent hours learning the details, here is the why and how. Credit goes to big-circle. The original question and answer here.
The problem is the cron doesn't have screen access.
Here's the solution
close SIP (System Integrity Protection).
Make sure your SIP is disabled. To check if SIP is disabled. To to terminal and type csrutil status. It should say SIP status: enabled/disabled.
To disable it:
Powerdown Mac, start up again and hold down cmd+r until OS X Utilities window shows up. Open terminal type csrutil disable. Restart again, boot into normal mac os.
grant write permission to TCC
sudo chmod 664 /Library/Application\ Support/com.apple.TCC
grant screencapure privilege to cron and screencapture
a) CRON:
`sudo sqlite3 "/Library/Application Support/com.apple.TCC/TCC.db" 'insert into access values ("kTCCServiceScreenCapture", "/usr/sbin/cron", 1, 1, 1, "", "", "", "UNUSED", "", 0,"")'`
b) screencapture:
Pre Big Sur:
`sudo sqlite3 "/Library/Application Support/com.apple.TCC/TCC.db" 'insert into access values ("kTCCServiceScreenCapture", "/usr/sbin/screencapture", 1, 1, 1, "", "", "", "UNUSED", "", 0,"")'`
--------------------------------------------------------------------------
Big Sur and later:
`sudo sqlite3 "/Library/Application Support/com.apple.TCC/TCC.db" 'insert into access values ("kTCCServiceScreenCapture", "/usr/sbin/screencapture", 1,2,4,1, "", "", "", "UNUSED", "", 0,"")'`
It's probably a good idea to turn SIP back on at this stage. To enable it, follow step 1, and instead of csrutil disable just type csrutil enable.
Edit # 2021-12-09T11:58:00+1000: Added in commands for Big Sur and later per comment from Silvan Mühlemann
You should allow your script to use the "Screen Recording" in the "Security & Privacy" system preferences.
You may start your app in a background from a terminal with a long loop (you need start it manually, that is not good):
#!/bin/bash
for value in {1..980}
do
/Users/<username>/record_activity_with_screen_capture.bash
sleep 60
done
echo All done
OR
use launchd (instead of cron) for running this script.
There are two examples:
how to start bash script in launchd
How to add timing
calendar example
Instead of playing with SIP (System Integrity Protection), which is dangerous, but suggested anyways but the top answer, simply give Screen Recording permissions to /usr/sbin/cron (or /sbin/cron in some MacOS versions I think).
In order to do that:
Open System Settings
Open Security & Privacy
Go to the Privacy tab
Go to Screen Recording
Click the lock in the bottom left corner and authenticate in order to enable editing the list
Now, if cron is listed there and unchecked, check it and the job is done. Likely is not listed there at all and you need to add it manually
Click on + below the list of programs
In the file picker hit a magic command Cmd+Shift+G, which will show a Go To Folder popup
Type: /usr/sbin and hit Enter (if you can't find cron in the next steps, try /sbin here instead)
You should see a list of files, all with an icon of a black terminal
Find cron on the list and hit Open
Check the checkbox next to cron
Wait for your cron to run the command again.
Voila, it should be capturing screenshots now.
You can try to change shebang to #!/bin/sh, or change screencapture to exec screencapture
Trying to kill an 2 unwanted process left over by a tech company. They both have to do with remote support. One is in root and the other is in user. Nothing is working.
Let's say the PID is 8005, here's what I have tried in terminal:
kill 8005, kill -9 8005, kill -KILL 8005, sudo kill 8005, sudo kill -9 8005, pkill -p -9 8005, sudo pkill -P -9 8005
And so on. You get the point.
Every time I attempt to kill, it comes back with a new PID almost instantly. The parent is launchd or kernaltask.
The tech company's response is "we have no idea." Please help!
This sounds a lot like Launchd, Apple's launch daemon manager.
You may or may not be able to remove it with the following steps:
Locate the pid of the live process
Enter launchctl list | grep PID into terminal while replacing "PID" with the pid of the process. The PIDs will be listed on the left, if you find the culprit in the list, skip to step 4, if not, continue.
Enter sudo launchctl list | grep PID into terminal and replace "PID" with the pid of the process like you did in step 2, you will most likely find a different list of processes this time because now you are filtering through the root daemons.
If you did not find the process identifier in the list, then the process is not being managed by launchctl (so sorry) and the rest of this answer is pretty irrelevant. If you did, however, find the pid: Continue.
The process should have (on the right side) a name in the format of com.blah.blah or something similar. Make sure to remember this.
If you found the pid in step 2:
Go in Finder, Press CMD+Shift+G and type or paste in ~/Library/LaunchAgents and hit "Go" it will bring you to a folder and look around in that folder for a file named "com.blah.blah" aka the name of the service. If you find the file, head over to the last and final step (at the bottom)
If you found the pid in step 4:
Go in Finder, Press CMD+Shift+G and type or paste in /Library/LaunchDaemons and hit "Go" it will bring you to a folder and look around in that folder for a file named "com.blah.blah" aka the name of the service. If you find the file, head over to the last and final step (at the bottom)
Provided you did not find the daemon in the prev step, repeat the last step only going to /Library/LaunchAgents after pressing "CMD+Shift+G" in Finder.
The final step, the one that makes it real!
At this point, you found the launch agent or daemon, and you're ready to terminate it. When you delete the file, after double checking everything, make sure that either you empty your trash immediately after, or delete it by pressing "CMD+Option+Delete", the reason why we do this, is to make sure that the file is actually gone from the OS, not just moved to the .Trashes folder. Depending on the type of service it is, you may need admin privileges, and also you may need to restart your computer.
Cheers, and good luck!
Edit/PS:
If any of you readers feel compelled to edit this answer/make it more clear, be my guest! I'm still learning the ropes here on SO and am doing my best to help others ;)
I might have a solution. Backup all of your important files, then follow this tutorial to reset your Mac: Click here to learn how to reset your mac back to original settings/data. You will lose your data, but any installed programs will dissapear and you will be able to start over.
I have a global launchd plist file that runs a backup script periodically. The script is a pretty simple bash script that sets up some variables and calls duply. However the plist doesn't seem to be preventing the system from going into sleep mode.
Is there a way of preventing the mac from going into sleep mode whilst this specific launchd plist and associated script are running?
The solution that seems to be working is
caffeinate -s [utility]
This forces the system to stay awake until the [utility] closes.
-s Create an assertion to prevent the system from sleeping. This assertion is valid only when sys-tem system
tem is running on AC power.
The full man page explains more.
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man8/caffeinate.8.html
I use the following to prevent my Mac sleeping:
pmset noidle
and then I use Ctrl-C to kill that and allow it to sleep again.
A notification center notification would be ideal but growl, bounce dock, sound, etc would be fine, too (or if this can only be done in Terminal.app I'd be willing to switch back). Is there an option somewhere in iTerm to turn on notifications or is it something I'm supposed to type at the end of a command in the terminal? If the latter, is it possible to add an alert once process has started (for example if I realize it's going to take longer than I initially expected, I'm bad at guessing).
Notify on an already running process:
Edit → Marks and Annotations → Alerts → Alert on next mark
or Shortcut: ⌥⌘A
: iTerm will literally keep an eye (on top right corner) of your terminal. Once the command is finished, it will issue a notification.
Requirement: Shell Integration:
iTerm2 → Install Shell Integration
NOTE: integration will not issue notifications until iTerm2 is restarted.
Example Use Case:
We launched a command, underestimated completion time, and we don't want to cancel or just wait for completion.
And you can always use the say command.
Usually when you are running a long process inside the terminal and want to get updated you can simply use this command to speak out things like done or error or bazinga.
mvn clean install; say done
This command builds a java spring app, and takes a long long time, and it will speak out done after the process is complete.
You can add any one of the following after any command, with a semi-colon in between the command and it:
afplay /System/Library/Sounds/Ping.aiff -v 2
osascript -e 'beep 3'
tput bel
or, if you like Notification Centre
osascript -e 'display notification "Lorem ipsum dolor sit amet" with title "Title"'
You can also make an alias in your profile, called notify and add that at the end of your command. So, in your login profile
alias notify="tput bel"
then
sleep 10; notify
Or, if you started your command and it is "hanging", just type notify and hit Enter and it will run your notify alias at the end, whne the command has finished, e.g.
sleep 20
# wait 5 seconds before realising this will take 20 seconds
notify<Enter>
iTerm2 supports Growl notifications. You can turn it on in each profile settings.
Select a profile in Preferences…->Profiles.
Then in Terminal tab there is an option Enable Growl Notifications.
Remember to also enable iTerm notifications in Growl preferences.
If you want to get notification for a given process you could try to experiment with Triggers. You define triggers in Advanced tab in a profile settings. In this way you may assign a Growl notification to a particular output of your process (regexp).
You could for example do:
$ mycommand; echo "end-of-my-process"
And connect trigger to "end-of-my-process" message.
Update
Read more about triggers on iTerm2.com.
There is an OSS tool called noti.
You can easily install it with brew install noti and start using it just by prefixing your command with noti like noti sleep 3.
Install the iTerm2 shell integration
curl -L https://iterm2.com/shell_integration/install_shell_integration_and_utilities.sh | bash
Execute your command and concatenate the attention app, e.g.
./task && ~/.iterm2/it2attention once
It'll cause the iTerm app to bounce it's icon once the job is complete.
You also have other attention options:
$ .iterm2/it2attention -h
Usage:
it2attention start
Begin bouncing the dock icon if another app is active
it2attention stop
Stop bouncing the dock icon if another app is active
it2attention once
Bounce the dock icon once if another app is active
it2attention fireworks
Show an explosion animation at the cursor
You can also use terminal-notifier which use mac os system notifications. To install it via Home brew just:
$ brew install terminal-notifier
Then if you want to display notification when your job/process is done use something like this
$ <your job/process command> && echo 'Completed' | terminal-notifier -sound default
And this display like this:
You can also change sound and icon of notifications. More info in github repo: https://github.com/julienXX/terminal-notifier
I have a LaunchDaemon. When it runs, it checks if SIMBL is installed. If SIMBL is not installed, it uses NSTask to run /usr/sbin/installer on the SIMBL.pkg.
SIMBL's postflight script then tries to run a launchctl load command to start SIMBL's LaunchAgent immediately:
sudo -u "$USER" -- /bin/launchctl load -F -S Aqua -D user "${LAUNCHD_PLIST}"
This fails, because my LaunchDaemon's NSTask environment doesn't have $USER set.
If I have my daemon detect the current user with the System Configuration framework and pass it to NSTask with setEnvironment, launchctl bugs out on me:
Bug: launchctl.c:2325 (23930):13: (dbfd = open(g_job_overrides_db_path, O_RDONLY | O_EXLOCK | O_CREAT, S_IRUSR | S_IWUSR)) != -1
I realize a daemon, by definition, should not operate in a user session. By the same token, Apple seems to recommend LaunchAgents as helper objects for LaunchDaemons, to do that user session work. Is there any way to get such an agent up and running immediately?
I have all the .plists in the right places (they start running after a reboot, the next time launchctl does its regular loading) so my first thought was to just tell launchctl to reload. But all the code to do that is commented out in launchctl.c:
// { "reload", reload_cmd, "Reload configuration files and/or directories" },
...
* In later versions of launchd, I hope to load everything in the first pass,
* then do the Bonjour magic on the jobs that need it, and reload them, but for now,
* I haven't thought through the various complexities of reloading jobs, and therefore
* launchd doesn't have reload support right now.
Oh how launchd drives me crazy....
To cut to the chase, after much study and experimentation, this is how I do it on 10.5+:
# If possible, tell launchd to start the LaunchAgent. This will fail on 10.4.
# $F[0] is the pid
# $F[1] is the username
# $F[2] is the first word of the command
ps -ww -A -opid,user,command | \
perl -nae 'if($F[2] =~ /\bloginwindow\b/) { system(
qq(launchctl bsexec $F[0] su $F[1] -c "launchctl load -w <your_plist>"))
}'
I have found no way to achieve this directly on 10.4. I cheat on 10.4 and just run the thing the LaunchAgent would have run, even though it has a GUI and you're not supposed to be able to do that (you can anyway in 10.4-10.6; you can't in 10.7). On 10.4, the LaunchAgent works correct after the next reboot.
The above code looks for loginwindow processes and uses bsexec to run commands in those contexts. Keep in mind that with Fast User Switching, there can be multiple contexts.
Some useful links:
Daemons and Services Programming Guide. You have to read it, but it won't actually answer any of the hard questions. But it will at least give you hints at where everything is located.
TN2083. This is a maddening document that raises as many questions as it answers, but is gospel and mandatory reading for anyone entering the abyss of launchd.
Starting/stopping a launchd agent for all users with GUI sessions. This has several other useful links and explanation.
IMO, launchd is one of the worst "great ideas" Apple has ever deployed. The idea is very useful, but the API is horrible.