|
| 1 | +// Copyright 2015 The Hugo Authors. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +// Copyright 2015 The Hugo Authors. All rights reserved. |
| 15 | +// |
| 16 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 17 | +// you may not use this file except in compliance with the License. |
| 18 | +// You may obtain a copy of the License at |
| 19 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 20 | +// |
| 21 | +// Unless required by applicable law or agreed to in writing, software |
| 22 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 23 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 24 | +// See the License for the specific language governing permissions and |
| 25 | +// limitations under the License. |
| 26 | + |
| 27 | +package commands |
| 28 | + |
| 29 | +import ( |
| 30 | + "syscall" |
| 31 | + |
| 32 | + "github.com/spf13/cobra" |
| 33 | + jww "github.com/spf13/jwalterweatherman" |
| 34 | +) |
| 35 | + |
| 36 | +func init() { |
| 37 | + checkCmd.AddCommand(limit) |
| 38 | +} |
| 39 | + |
| 40 | +var limit = &cobra.Command{ |
| 41 | + Use: "ulimit", |
| 42 | + Short: "Check system ulimit settings", |
| 43 | + Long: `Hugo will inspect the current ulimit settings on the system. |
| 44 | +This is primarily to ensure that Hugo can watch enough files on some OSs`, |
| 45 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 46 | + var rLimit syscall.Rlimit |
| 47 | + err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) |
| 48 | + if err != nil { |
| 49 | + return newSystemError("Error Getting Rlimit ", err) |
| 50 | + } |
| 51 | + |
| 52 | + jww.FEEDBACK.Println("Current rLimit:", rLimit) |
| 53 | + |
| 54 | + jww.FEEDBACK.Println("Attempting to increase limit") |
| 55 | + rLimit.Max = 999999 |
| 56 | + rLimit.Cur = 999999 |
| 57 | + err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) |
| 58 | + if err != nil { |
| 59 | + return newSystemError("Error Setting rLimit ", err) |
| 60 | + } |
| 61 | + err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) |
| 62 | + if err != nil { |
| 63 | + return newSystemError("Error Getting rLimit ", err) |
| 64 | + } |
| 65 | + jww.FEEDBACK.Println("rLimit after change:", rLimit) |
| 66 | + |
| 67 | + return nil |
| 68 | + }, |
| 69 | +} |
| 70 | + |
| 71 | +func tweakLimit() { |
| 72 | + var rLimit syscall.Rlimit |
| 73 | + err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) |
| 74 | + if err != nil { |
| 75 | + jww.ERROR.Println("Unable to obtain rLimit", err) |
| 76 | + } |
| 77 | + if rLimit.Cur < rLimit.Max { |
| 78 | + rLimit.Max = 999999 |
| 79 | + rLimit.Cur = 999999 |
| 80 | + err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) |
| 81 | + if err != nil { |
| 82 | + jww.ERROR.Println("Unable to increase number of open files limit", err) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments