Author Archives: Manila Chaturvedi

How to persist ulimit settings in OSX

Shell Session Limit

The limits set via ulimit only affects processes created by the current shell session.

  • The “soft limit” is the actual limit that is used. It could be set, as far as it’s not greater than the “hard limit”.
  • The “hard limit” could also be set, but only to a value less than the current one, and only to a value not less than the “soft limit”.
  • The “hard limit”, as well as system-wide limits, could be raised by root (the administrator) by executing system configuration commands or modifying system configuration files.

After you terminate the shell session (by Ctrl+D, exit, or closing the Terminal.app window, etc.), the settings are gone. If you want the same setting in the next shell session, add the setting to the shell startup script.

NOTE: If you are using bash, then it should be ~/.bash_proile or ~/.bash_login. If you are using other shells, it should probably be ~/.profile.

System Limit (Requires Reboot to Take Effect)

For older Mac OS X (I guess it works on 10.7 (Lion) or before):You may add the following line to /etc/launchd.conf (owner: root:wheel, mode: 0644):

limit maxfiles 262144 524288

You should change the numbers according to your needs. They are the “soft limit” (262144) and the is “hard limit” (524288) respectively.

For 10.8 (Mountain Lion):You may add the following lines to /etc/sysctl.conf (owner: root:wheel, mode: 0644):

kern.maxfiles=524288
kern.maxfilesperproc=262144

You should change the numbers according to your needs. They are the “system-wide limit” (kern.maxfiles) and “per-process limit” (kern.maxfilesperproc) respectively. For more settings, consult the manual page by running man sysctl, or read the source code at /usr/include/sys/sysctl.h.

For 10.9 (Mavericks), 10.10 (Yosemite),  10.11 (El Capitan), and 10.12 (Sierra):You have to create a file at /Library/LaunchDaemons/limit.maxfiles.plist (owner: root:wheel, mode: 0644):

<?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>limit.maxfiles</string>
    <key>ProgramArguments</key>
    <array>
      <string>launchctl</string>
      <string>limit</string>
      <string>maxfiles</string>
      <string>262144</string>
      <string>524288</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>ServiceIPC</key>
    <false/>
  </dict>
</plist>

You should change the numbers according to your needs. They are the “soft limit” (262144) and the is “hard limit” (524288) respectively. For more information, consult the manual page by running man launchd.plist.

If the system doesn’t let you set the limits above a certain value…The system doesn’t let you set a value higher than a “hard maximum” (proposed by Apple). To increase this “hard maximum”, you have to purchase “OS X Server” from “App Store”, then you have to execute the following command once:

sudo serverinfo --setperfmode true

This activates “server performance mode” on your machine. You can then set the maximum according to the configuration of your machine (see this).

References

PrestaShop – Setup TEST site in sub-domain

Clone production DB to a new database and run following query in new DB


UPDATE `ps_shop` SET `name`=concat('TEST ',`name`);
UPDATE `ps_shop_url` SET `domain`=concat('test.',`domain`),`domain_ssl`=concat('test.',`domain_ssl`);
UPDATE `ps_configuration` SET `value` = concat('test.',`value`) WHERE `name` like 'PS_SHOP_DOMAIN%';
UPDATE `ps_configuration` SET `value` = '0' WHERE `name` = 'PS_SHOP_ENABLE';
UPDATE `ps_configuration` SET  `value`=concat('TEST ',`value`) WHERE `name` = 'PS_SHOP_NAME';
UPDATE `ps_configuration` SET value = '2' where `name` = 'PS_SMARTY_FORCE_COMPILE';
UPDATE `ps_configuration` SET value = '0' where `name` = 'PS_SMARTY_CACHE';

You can run following query to avoid sending mails to customer for any test:


UPDATE `ps_customer` SET `email` = CONCAT(REPLACE(REPLACE(`email` ,'@','-'),'.','_'),'@mydomain.com');

 

MySQL on Mac OSX change sql_mode in LaunchDaemons

After installing MySQL for Mac OSX a launch daemon file (/Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist) is created. Adding lines mentioned in Orange will force mysql daemon to use custom option.

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE plist PUBLIC “-//Apple Computer//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
<plist version=”1.0″>
<dict>
<key>Label</key>             <string>com.oracle.oss.mysql.mysqld</string>
<key>ProcessType</key>       <string>Interactive</string>
<key>Disabled</key>          <false/>
<key>RunAtLoad</key>         <true/>
<key>KeepAlive</key>         <true/>
<key>SessionCreate</key>     <true/>
<key>LaunchOnlyOnce</key>    <false/>
<key>UserName</key>          <string>_mysql</string>
<key>GroupName</key>         <string>_mysql</string>
<key>ExitTimeOut</key>       <integer>600</integer>
<key>Program</key>           <string>/usr/local/mysql/bin/mysqld</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/mysql/bin/mysqld</string>
<string>–user=_mysql</string>
<string>–sql_mode=NO_ENGINE_SUBSTITUTION</string>
            <string>–open_files_limit=100000</string>
            <string>–wait_timeout=600000</string>
            <string>–max_allowed_packet=16M</string>
<string>–basedir=/usr/local/mysql</string>
<string>–datadir=/usr/local/mysql/data</string>
<string>–plugin-dir=/usr/local/mysql/lib/plugin</string>
<string>–log-error=/usr/local/mysql/data/mysqld.local.err</string>
<string>–pid-file=/usr/local/mysql/data/mysqld.local.pid</string>
</array>
<key>WorkingDirectory</key>  <string>/usr/local/mysql</string>
</dict>
</plist>

MySQL start manually with diff user and data

If you have recently update mysql database and forgot to make a backup of old database then use following technique.

  1. Download TAR version my MySQL matching the old version
  2. untar the MySQL program
  3. stop new version of mysql server
  4. start old version of mysql using “mysqld –user=mysql –console –datadir=/usr/local/mysql-5.6.25-osx10.8-x86_64/data
  5. Now old version server is up so you can continue export databases
  6. stop mysqld
  7. start new MySQl server
  8. Restore database using newly backed up files.

Start Tomcat automatically on OS X using launchd

Using launchd with Tomcat

The Tomcat standalone distribution ships with Tomcat. There is a mismatch between how launchd expects a daemon to behave, and how the default startup scripts for Tomcat operate:

  • OS X’s launchd expects the process it starts to run forever, but ‘catalina.sh start‘ starts the JVM to run Tomcat and then exits.
  • Tomcat provides ‘catalina.sh stop‘ to shut down Tomcat cleanly by connecting to a socket which Tomcat listens on, but launchd stops daemons by sending them a signal that kills the process immediately if no specific handling is included.

You will need a wrapper shell script and properties list to make launchd work with Tomcat.

Step 1. Add a Wrapper Shell Script

Add the following wrapper shell script to $CATALINA_HOME/bin:

launchd_wrapper.sh
#!/bin/bash
function shutdown()
{
        date
        echo "Shutting down Tomcat"
        $CATALINA_HOME/bin/catalina.sh stop
}
date
echo "Starting Tomcat"
export CATALINA_PID=/tmp/$$
# Uncomment to increase Tomcat's maximum heap allocation
# export JAVA_OPTS=-Xmx512M $JAVA_OPTS
. $CATALINA_HOME/bin/catalina.sh start
# Allow any signal which would kill a process to stop Tomcat
trap shutdown HUP INT QUIT ABRT KILL ALRM TERM TSTP
echo "Waiting for `cat $CATALINA_PID`"
wait `cat $CATALINA_PID`

 

The above shell script starts Tomcat and then waits for the process to complete, so launchd is happy that Tomcat is still running. The script also installs a signal handler, which calls the shutdown() function to cleanly shut down Tomcat when launchd signals the script.

You can try this script manually: Start the script, watch Tomcat start, and then type ctrl-C and see Tomcat shut down cleanly. (Note that it will not shut down cleanly if Tomcat has not started yet. It takes a few seconds for Tomcat to start listening on the shutdown socket.)

Step 2. Add a launchd Property List

The launchd property list (.plist) tells launchd how to start Tomcat.

Add the following plist file to /Library/LaunchDaemons, which is the location for system-wide services which are not part of base OS X:

tomcat.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Disabled</key>
        <false/>
        <key>EnvironmentVariables</key>
        <dict>
                <key>CATALINA_HOME</key>
                <string>/Users/myname/conf/tomcat-6.0</string>
                <key>JAVA_HOME</key>
                <string>/Library/Java/Home</string>
        </dict>
        <key>Label</key>
        <string>tomcat</string>
        <key>OnDemand</key>
        <false/>
        <key>ProgramArguments</key>
        <array>
                <string>/Users/myname/conf/tomcat-6.0/bin/launchd_wrapper.sh</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>ServiceDescription</key>
        <string>Tomcat</string>
        <key>StandardErrorPath</key>
        <string>/Users/myname/conf/tomcat-6.0/logs/launchd.stderr</string>
        <key>StandardOutPath</key>
        <string>/Users/myname/conf/tomcat-6.0/logs/launchd.stdout</string>
        <key>UserName</key>
        <string>root</string>
</dict>
</plist>

Notes:

  1. Replace ‘/Users/myname/conf/tomcat-6.0‘ with the path to your Tomcat installation. The string occurs four times in the above script.
  2. JAVA_HOME is set to use the default JDK. On OS X version 10.4.4, the default JDK is 1.4.2. You will need to change this value if you want to use a different version of Java. For example, if you want to use JDK 1.5, you will need to change JAVA_HOME to /System/Library/Frameworks/JavaVM.framework/Versions/1.5.
  3. In the above script, we have specified ‘root‘ as the UserName. If necessary, change the UserName to the user you want Tomcat to run as.

Starting and Stopping Tomcat Manually

To start and stop Tomcat manually, use the following commands:

  • Start:
    cd /Library/LaunchDaemons
    sudo launchctl load -w tomcat.plist
  • Stop:
    cd /Library/LaunchDaemons
    sudo launchctl unload -w tomcat.plist

Troubleshooting

  • Make sure both files launch_wrapper.sh and tomcat.plist have the necessary file privileges.
  • Check the console logging and log file for any abnormalities.