How can I run this code on startup?

rimmi2002

macrumors member
Original poster
Apr 6, 2011
93
0
Hi I wanted to run this code on each startup:
sudo pmset womp 0

Can someone please tell me how can I do it? Thanks.
 

rimmi2002

macrumors member
Original poster
Apr 6, 2011
93
0
K I used sudo nana and the pasted the command in there. Saved the file and gave it the following permissions sudo chmod +x filename.

I added the file to startup script. It runs the command in terminal now, but asks for my password to execute on each startup. Anyway I can bypass the ask password step on provide the password in the command? Please advise. Do I need to allow different permissions to the file?
 

haravikk

macrumors 65816
May 1, 2005
1,487
19
The best way to do it is with launchd, it's not quite as simple as a login or startup item, but by creating it in /Library/LaunchDaemons it can run as root.

The file you'd want to create would look something like:


Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
	<dict>
		<key>Label</key>
		<string>com.pmset</string>
		<key>ProgramArguments</key>
		<array>
			<string>pmset</string>
			<string>womp</string>
			<string>0</string>
		</array>
		<key>RunAtLoad</key>
		<true/>
	</dict>
</plist>
Just create the file in that format under something like:
/Library/LaunchDaemons/com.pmset.plist

This should cause it to run invisibly, and you can use the label of com.pmset to check that it's running in the Console.

While it's a bit more of a learning curve, I think that launchd is the best way to run anything at start-up, on a specific date (daily, weekly etc.), or for watching out for volume changes (mount) or folder updates. I use it now for all sorts of things, including setting the noatime flag on some volumes, checking if Macports needs updating and so-on.
 

rimmi2002

macrumors member
Original poster
Apr 6, 2011
93
0
The best way to do it is with launchd, it's not quite as simple as a login or startup item, but by creating it in /Library/LaunchDaemons it can run as root.

The file you'd want to create would look something like:


Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
	<dict>
		<key>Label</key>
		<string>com.pmset</string>
		<key>ProgramArguments</key>
		<array>
			<string>pmset</string>
			<string>womp</string>
			<string>0</string>
		</array>
		<key>RunAtLoad</key>
		<true/>
	</dict>
</plist>
Just create the file in that format under something like:
/Library/LaunchDaemons/com.pmset.plist

This should cause it to run invisibly, and you can use the label of com.pmset to check that it's running in the Console.

While it's a bit more of a learning curve, I think that launchd is the best way to run anything at start-up, on a specific date (daily, weekly etc.), or for watching out for volume changes (mount) or folder updates. I use it now for all sorts of things, including setting the noatime flag on some volumes, checking if Macports needs updating and so-on.
Thanks. I tried this. The files exist in launchdameons, but does nothing. I find not mention of it in the console in all messages section and It does not turn off wake on network, like it suppose to. Is there anything else I can do to make it work?
 

larkost

macrumors 6502a
Oct 13, 2007
536
1
The most probable problem is that you don't have the permissions right on the file. It should be owned by root, and needs to be readable by root, and not writeable by anyone other than root. The following commands should take care of that:
Code:
sudo chown root:wheel /Library/LaunchDaemons/com.pmset.plist
sudo chmod 755 /Library/LaunchDaemons/com.pmset.plist
Note that I am assuming the example path given above (which I would never normall use... it should not be "com.", but something like "personal.pmset.plist"). You can check if it has run with:
Code:
sudo launchctl list | grep 'com.pmset'
If it has sucessfully run there should be a '-' in the first column, and a '0' in the second. Otherwise you might want to look in the system log.
 

rimmi2002

macrumors member
Original poster
Apr 6, 2011
93
0
The most probable problem is that you don't have the permissions right on the file. It should be owned by root, and needs to be readable by root, and not writeable by anyone other than root. The following commands should take care of that:
Code:
sudo chown root:wheel /Library/LaunchDaemons/com.pmset.plist
sudo chmod 755 /Library/LaunchDaemons/com.pmset.plist
Note that I am assuming the example path given above (which I would never normall use... it should not be "com.", but something like "personal.pmset.plist"). You can check if it has run with:
Code:
sudo launchctl list | grep 'com.pmset'
If it has sucessfully run there should be a '-' in the first column, and a '0' in the second. Otherwise you might want to look in the system log.
Thanks for the help. I guess it does run, because I got a - and 0 which using the GREP function. How it doesn't do what its suppose to do. Something in my system always enables wake on network on startup. I am not sure what it is...When I uncheck it..its rechecked on startup. So command might be running before the other function that is enabling it on each startup.
 

rimmi2002

macrumors member
Original poster
Apr 6, 2011
93
0
Just figured it out. The stupid problem was from Logmein...which had wake on LAN enabled. So each time or system start it would enable wake on lan. The script was probably running before Logmein started and thats why it reverted back to pmset womp 1. Thanks for the help!
 

haravikk

macrumors 65816
May 1, 2005
1,487
19
Oh, sorry about the permissions trouble, I forget I always use sudo to do anything Launch Daemon related! As for the naming convention, it doesn't really matter what it's called, as long as it's unique, I usually put something like "com.haravikk.<process name>", but it doesn't really matter either way.

Anyway, glad to here it's working for you!