質問編集履歴

9

タグの追加

2016/11/14 08:04

投稿

dlrowolleh
dlrowolleh

スコア120

title CHANGED
File without changes
body CHANGED
@@ -67,4 +67,26 @@
67
67
  No mail for (ユーザ名)
68
68
  ```
69
69
 
70
- 何卒よろしくお願いします
70
+ 何卒よろしくお願いします
71
+
72
+ #追記
73
+ `$ cat /var/log/cron`
74
+ `cat: /var/log/cron: そのようなファイルやディレクトリはありません`
75
+ と表示されました。
76
+ 調べていくうちに
77
+ `/etc/init.d/cron start`
78
+ と書いてあり、
79
+ [Ubuntu@11 決まった時間に自動で電源を切る cronでshutdown - Cotton ...](http://cpw.hatenablog.com/entry/20120425/1335280082)
80
+
81
+ `$ vi /etc/crontab `
82
+ `$ /etc/init.d/cron start`
83
+ `[ ok ] Starting cron (via systemctl): cron.service.`
84
+
85
+ やってみたのですが、
86
+ 電源は切れませんでした。
87
+
88
+ `$ mail`
89
+ `No mail for h28-hashimoto`
90
+ `$ cat /var/log/cron
91
+ cat: /var/log/cron: そのようなファイルやディレクトリはありません`
92
+ でした。

8

編集

2016/11/14 08:04

投稿

dlrowolleh
dlrowolleh

スコア120

title CHANGED
@@ -1,1 +1,1 @@
1
- Cronで自動シャットダウンできない
1
+ 【Ubuntu】Cronで自動シャットダウンできない
body CHANGED
File without changes

7

修正

2016/11/14 04:42

投稿

dlrowolleh
dlrowolleh

スコア120

title CHANGED
@@ -1,1 +1,1 @@
1
- LinuxのPCを時間を指定して自動シャットダウンする方法を教えてくださ
1
+ Cronで自動シャットダウンできな
body CHANGED
@@ -3,220 +3,68 @@
3
3
  ###前提・実現したいこと→LinuxのPCを時間を指定して自動でシャットダウンしたい
4
4
  LinuxのPCを時間を指定して自動でシャットダウンする方法を教えてください。
5
5
  ###試したこと
6
- 0. シャットダウンコマンドをcronで実行しようとしました。
7
- 参考:[linuxの自動シャットダウンについて教えてください
8
- ](http://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q1470731414)
9
- ```
10
- $ crontab -e
6
+ `$ crontab -e`
11
- 40 19 * * * shutdown -h now
12
- crontab: installing new crontab
13
-
14
- ```
15
- では
16
- 時間になっても電源は切れませんでした。
17
- また、
18
-
19
- ```
20
- $ mail
21
-
22
- ```
23
- でエラーの内容を見たところ、
24
-
25
- ```
26
- /bin/sh: 1: shutdown: not found
27
-
28
- ```
29
- でした。
30
-
31
- そのため、
32
-
33
- 0. シャットダウンするスクリプトを作成してcronでそのスクリプトを実行しようとしました。
34
-
35
- ```
36
- $ vi Desktop/2016/sample/shutdown.sh
37
-
38
- ```
39
-
40
- ```bash
41
- #!/bin/bash -x
42
- shutdown -h now
43
-
44
- ```
45
-
46
- ```
47
- $ chmod +x shutdown.sh
48
-
49
- ```
50
- で実行権限を与え
51
-
52
- ```
53
- $ crontab -e
54
-
55
- ```
56
7
 
57
8
 
58
9
  ```
10
+ # Edit this file to introduce tasks to be run by cron.
11
+ #
12
+ # Each task to run has to be defined through a single line
13
+ # indicating with different fields when the task will be run
14
+ # and what command to run for the task
15
+ #
16
+ # To define the time you can provide concrete values for
17
+ # minute (m), hour (h), day of month (dom), month (mon),
18
+ # and day of week (dow) or use '*' in these fields (for 'any').#
19
+ # Notice that tasks will be started based on the cron's system
20
+ # daemon's notion of time and timezones.
21
+ #
22
+ # Output of the crontab jobs (including errors) is sent through
23
+ # email to the user the crontab file belongs to (unless redirected).
24
+ #
25
+ # For example, you can run a backup of all your user accounts
26
+ # at 5 a.m every week with:
59
- 08 20 * * * cd /home/h28-sotuken/Desktop/2016/sample; ./shutdown.sh
27
+ # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
28
+ #
29
+ # For more information see the manual pages of crontab(5) and cron(8)
30
+ #
31
+ # m h dom mon dow command
60
32
 
33
+ PATH=/bin:/sbin:/usr/bin:/usr/sbin
34
+ 27 13 * * * shutdown -h now
61
35
  ```
62
- としましたが時間になっても電源は切れませんでした。
36
+ ではシャットダウンできませんでした。
63
-
64
- ```
65
- $ mail
37
+ `$ mail`
66
-
67
- ```
68
- でエラーの内容を見たところ、
69
-
70
- ```
71
- + shutdown -h now
72
- ./shutdown.sh: 行 3: shutdown: コマンドが見つかりません
73
-
74
- ```
75
- しかし、
76
-
77
- ```
78
- $ cd /home/(ユーザ名)/Desktop/2016/sample; ./shutdown.sh
79
-
80
- ```
81
38
  では
82
- rootのパスワードを聞かれず(何故だか気になりますが…)電源が切れました
83
-
84
- ###補足情報(言語/FW/ツール等のバージョンなど)
85
- Ubutu15.10です。
86
- デスクトップ環境です。
87
-
88
- 何卒よろしくお願いします。
89
- ###追記(11/11)
90
- `crontab -e`
91
-
92
39
  ```
93
- 01 15 * * * /sbin/shutdown -h now
94
- ```
95
- と編集してみたところ、
96
- シャットダウンできませんでした。
97
- `$ mail`
98
- ```
99
40
  Failed to execute operation: Interactive authentication required.
100
41
  Must be root.
101
42
  ```
102
- と、エラーメールが来ました。
103
- [cronジョブを作るのにいつものやり方でいいんですか? - Qiita](http://qiita.com/jmatsu/items/0a5d80abe188b09644c1)
104
- という情報を見つけ、
105
- `$ vi /etc/crontab `
106
- をやってみたところ
107
- ```
108
- # /etc/crontab: system-wide crontab$
109
- # Unlike any other crontab you don't have to run the `crontab'$
110
- # command to install the new version when you edit this file$
111
- # and files in /etc/cron.d. These files also have username fields,$
112
- # that none of the other crontabs do.$
113
- $
114
- SHELL=/bin/sh$
115
- PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin$
116
- $
117
- # m h dom mon dow user^Icommand$
118
- 17 *^I* * *^Iroot cd / && run-parts --report /etc/cron.hourly$
119
- 25 6^I* * *^Iroot^Itest -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )$
120
- 47 6^I* * 7^Iroot^Itest -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )$
121
- 52 6^I1 * *^Iroot^Itest -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )$
122
- #自分で追加した項目$
123
- 40 18 * * * root /sbin/shutdown -h now$
124
- ```
125
- となっていました。
43
+ した。
126
44
 
127
- また、
45
+ `$ vi /etc/crontab`
128
46
  ```
47
+ # /etc/crontab: system-wide crontab
48
+ # Unlike any other crontab you don't have to run the `crontab'
49
+ # command to install the new version when you edit this file
50
+ # and files in /etc/cron.d. These files also have username fields,
129
- $ which reboot
51
+ # that none of the other crontabs do.
52
+
130
- /sbin/reboot
53
+ SHELL=/bin/sh
54
+ PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
55
+
56
+ # m h dom mon dow user>-command
57
+ 17 *>---* * *>--root cd / && run-parts --report /etc/cron.hourly
58
+ 25 6>---* * *>--root>---test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
59
+ 47 6>---* * 7>--root>---test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
60
+ 52 6>---1 * *>--root>---test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
61
+
62
+ 30 13 * * * root /sbin/shutdown -h now
131
63
  ```
132
- ```
133
- $ which shutdown
134
- /sbin/shutdown
135
- ```
136
- また、
137
- `$ sudo visudo
138
- [sudo] password for (ユーザ名):`
139
- ```
140
- #$
141
- # This file MUST be edited with the 'visudo' command as root.$
142
- #$
143
- # Please consider adding local content in /etc/sudoers.d/ instead of$
144
- # directly modifying this file.$
145
- #$
146
- # See the man page for details on how to write a sudoers file.$
147
- #$
148
- Defaults^Ienv_reset$
64
+ でもシャットダウンできませんでした。
149
- Defaults^Imail_badpass$
150
- Defaults^Isecure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"$
151
- $
152
- # Host alias specification$
153
- $
154
- # User alias specification$
155
- $
156
- # Cmnd alias specification$
157
- $
158
- # User privilege specification$
159
- root^IALL=(ALL:ALL) ALL$
160
- $
161
- # Members of the admin group may gain root privileges$
162
- %admin ALL=(ALL) ALL$
163
- $
164
- # Allow members of group sudo to execute any command$
165
- %sudo^IALL=(ALL:ALL) ALL$
166
- $
167
- # See sudoers(5) for more information on "#include" directives:$
168
- $
169
- #includedir /etc/sudoers.d$
170
- ```
171
-
172
- `visudo: /etc/sudoers.tmp は変更されません`
173
- となりました。
174
-
175
- `$ crontab -e`
176
-
177
- `05 19 * * * root /sbin/shutdown -h now`
178
- としてみたところ
179
65
  `$ mail`
180
- で確認すると
181
- `/bin/sh: 1: root: not found`
182
- となっていました。
183
-
184
- また、
185
- `$ vi /etc/crontab`
186
-
187
- `20 19 * * * root /sbin/shutdown -h now`
188
- としてみたのですが、
189
- シャットダウンできませんでした。
190
- `$ mail`
191
- で確認すると
192
- `No mail for (ユーザ名)`
193
- でした。
194
-
195
- #追記
196
- `$ sudo visudo`
197
-
198
66
  ```
199
- hoge2-hasimoto ALL = NOPASSWD: /sbin/shutdown -h now
67
+ No mail for (ユーザ名)
200
68
  ```
201
- と設定しました。(行末ですが大丈夫?)
202
- ちなみに、
203
- ```
204
- $ echo $USER
205
- hoge2-hasimoto
206
- ```
207
- です(文字に色がつかなかったのにものすごい違和感を感じます…。)
208
- その後、
209
- `$ crontab -e`
210
-
211
69
 
212
- `31 20 * * * /sbin/shutdown -h now`
213
-
214
-
215
- `$ mail`
216
- で確認すると
217
- `Failed to execute operation: Interactive authentication required.
218
- Must be root.`
219
- でした。
220
-
221
- 長くなりましてすみません。
222
70
  何卒よろしくお願いします

6

編集

2016/11/14 04:41

投稿

dlrowolleh
dlrowolleh

スコア120

title CHANGED
File without changes
body CHANGED
@@ -190,4 +190,33 @@
190
190
  `$ mail`
191
191
  で確認すると
192
192
  `No mail for (ユーザ名)`
193
- でした。
193
+ でした。
194
+
195
+ #追記
196
+ `$ sudo visudo`
197
+
198
+ ```
199
+ hoge2-hasimoto ALL = NOPASSWD: /sbin/shutdown -h now
200
+ ```
201
+ と設定しました。(行末ですが大丈夫?)
202
+ ちなみに、
203
+ ```
204
+ $ echo $USER
205
+ hoge2-hasimoto
206
+ ```
207
+ です(文字に色がつかなかったのにものすごい違和感を感じます…。)
208
+ その後、
209
+ `$ crontab -e`
210
+
211
+
212
+ `31 20 * * * /sbin/shutdown -h now`
213
+
214
+
215
+ `$ mail`
216
+ で確認すると
217
+ `Failed to execute operation: Interactive authentication required.
218
+ Must be root.`
219
+ でした。
220
+
221
+ 長くなりましてすみません。
222
+ 何卒よろしくお願いします

5

報告

2016/11/11 11:43

投稿

dlrowolleh
dlrowolleh

スコア120

title CHANGED
File without changes
body CHANGED
@@ -179,4 +179,15 @@
179
179
  `$ mail`
180
180
  で確認すると
181
181
  `/bin/sh: 1: root: not found`
182
- となっていました。
182
+ となっていました。
183
+
184
+ また、
185
+ `$ vi /etc/crontab`
186
+
187
+ `20 19 * * * root /sbin/shutdown -h now`
188
+ としてみたのですが、
189
+ シャットダウンできませんでした。
190
+ `$ mail`
191
+ で確認すると
192
+ `No mail for (ユーザ名)`
193
+ でした。

4

追記修正

2016/11/11 10:24

投稿

dlrowolleh
dlrowolleh

スコア120

title CHANGED
File without changes
body CHANGED
@@ -83,6 +83,7 @@
83
83
 
84
84
  ###補足情報(言語/FW/ツール等のバージョンなど)
85
85
  Ubutu15.10です。
86
+ デスクトップ環境です。
86
87
 
87
88
  何卒よろしくお願いします。
88
89
  ###追記(11/11)

3

tuiki

2016/11/11 10:17

投稿

dlrowolleh
dlrowolleh

スコア120

title CHANGED
File without changes
body CHANGED
@@ -169,4 +169,13 @@
169
169
  ```
170
170
 
171
171
  `visudo: /etc/sudoers.tmp は変更されません`
172
- となりました。
172
+ となりました。
173
+
174
+ `$ crontab -e`
175
+
176
+ `05 19 * * * root /sbin/shutdown -h now`
177
+ としてみたところ
178
+ `$ mail`
179
+ で確認すると
180
+ `/bin/sh: 1: root: not found`
181
+ となっていました。

2

修正

2016/11/11 10:16

投稿

dlrowolleh
dlrowolleh

スコア120

title CHANGED
File without changes
body CHANGED
@@ -169,40 +169,4 @@
169
169
  ```
170
170
 
171
171
  `visudo: /etc/sudoers.tmp は変更されません`
172
- となりました。
172
+ となりました。
173
- #余談
174
-
175
- 0. `$ reboot`でパスワードを求められず再起動ができる(sudoなし)
176
- 0. `$ shutdown`でパスワードを求められずシャットダウンができる(sudoなし)
177
- 0. (シャットダウンとは関係ないがsudoに関して)
178
-
179
- `$ sudo apt-get update`←sudo実行
180
- `[sudo] password for (ユーザ名):`←パスワードを聞かれる
181
- 〜省略(処理中)〜
182
- `パッケージリストを読み込んでいます... 完了`
183
-
184
-
185
- `$ sudo apt-get update`←sudo実行(パスワードを聞かれない)
186
- 〜省略(処理中)〜
187
- `パッケージリストを読み込んでいます... 完了`
188
-
189
-
190
- `$ apt-get update`←sudo実行してない
191
- 〜省略(処理中)〜
192
- `パッケージリストを読み込んでいます... 完了`
193
-
194
- `$ exit`←端末終了
195
- 〜端末起動〜
196
- `$ sudo apt-get update`←sudo実行
197
- `[sudo] password for (ユーザ名):`←パスワードを聞かれる
198
- 〜省略(処理中)〜
199
- `パッケージリストを読み込んでいます... 完了`
200
-
201
- というsudoが本来なら必要(?)な処理をする際にsudoをつけなくても処理が始まったり、
202
- sudo実行後にはパスワードを聞かれるはず(?)なのにパスワードを聞かれずに処理が始まる
203
-
204
- という事態が発生しています。
205
- 原因はなんでしょうか…。(これが普通なのか?)
206
- 少し気になります。
207
- たくさん書いてしまいすみません。
208
- 何卒よろしくお願いします。

1

追記修正

2016/11/11 08:02

投稿

dlrowolleh
dlrowolleh

スコア120

title CHANGED
File without changes
body CHANGED
@@ -84,4 +84,125 @@
84
84
  ###補足情報(言語/FW/ツール等のバージョンなど)
85
85
  Ubutu15.10です。
86
86
 
87
+ 何卒よろしくお願いします。
88
+ ###追記(11/11)
89
+ `crontab -e`
90
+
91
+ ```
92
+ 01 15 * * * /sbin/shutdown -h now
93
+ ```
94
+ と編集してみたところ、
95
+ シャットダウンできませんでした。
96
+ `$ mail`
97
+ ```
98
+ Failed to execute operation: Interactive authentication required.
99
+ Must be root.
100
+ ```
101
+ と、エラーメールが来ました。
102
+ [cronジョブを作るのにいつものやり方でいいんですか? - Qiita](http://qiita.com/jmatsu/items/0a5d80abe188b09644c1)
103
+ という情報を見つけ、
104
+ `$ vi /etc/crontab `
105
+ をやってみたところ
106
+ ```
107
+ # /etc/crontab: system-wide crontab$
108
+ # Unlike any other crontab you don't have to run the `crontab'$
109
+ # command to install the new version when you edit this file$
110
+ # and files in /etc/cron.d. These files also have username fields,$
111
+ # that none of the other crontabs do.$
112
+ $
113
+ SHELL=/bin/sh$
114
+ PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin$
115
+ $
116
+ # m h dom mon dow user^Icommand$
117
+ 17 *^I* * *^Iroot cd / && run-parts --report /etc/cron.hourly$
118
+ 25 6^I* * *^Iroot^Itest -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )$
119
+ 47 6^I* * 7^Iroot^Itest -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )$
120
+ 52 6^I1 * *^Iroot^Itest -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )$
121
+ #自分で追加した項目$
122
+ 40 18 * * * root /sbin/shutdown -h now$
123
+ ```
124
+ となっていました。
125
+
126
+ また、
127
+ ```
128
+ $ which reboot
129
+ /sbin/reboot
130
+ ```
131
+ ```
132
+ $ which shutdown
133
+ /sbin/shutdown
134
+ ```
135
+ また、
136
+ `$ sudo visudo
137
+ [sudo] password for (ユーザ名):`
138
+ ```
139
+ #$
140
+ # This file MUST be edited with the 'visudo' command as root.$
141
+ #$
142
+ # Please consider adding local content in /etc/sudoers.d/ instead of$
143
+ # directly modifying this file.$
144
+ #$
145
+ # See the man page for details on how to write a sudoers file.$
146
+ #$
147
+ Defaults^Ienv_reset$
148
+ Defaults^Imail_badpass$
149
+ Defaults^Isecure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"$
150
+ $
151
+ # Host alias specification$
152
+ $
153
+ # User alias specification$
154
+ $
155
+ # Cmnd alias specification$
156
+ $
157
+ # User privilege specification$
158
+ root^IALL=(ALL:ALL) ALL$
159
+ $
160
+ # Members of the admin group may gain root privileges$
161
+ %admin ALL=(ALL) ALL$
162
+ $
163
+ # Allow members of group sudo to execute any command$
164
+ %sudo^IALL=(ALL:ALL) ALL$
165
+ $
166
+ # See sudoers(5) for more information on "#include" directives:$
167
+ $
168
+ #includedir /etc/sudoers.d$
169
+ ```
170
+
171
+ `visudo: /etc/sudoers.tmp は変更されません`
172
+ となりました。
173
+ #余談
174
+
175
+ 0. `$ reboot`でパスワードを求められず再起動ができる(sudoなし)
176
+ 0. `$ shutdown`でパスワードを求められずシャットダウンができる(sudoなし)
177
+ 0. (シャットダウンとは関係ないがsudoに関して)
178
+
179
+ `$ sudo apt-get update`←sudo実行
180
+ `[sudo] password for (ユーザ名):`←パスワードを聞かれる
181
+ 〜省略(処理中)〜
182
+ `パッケージリストを読み込んでいます... 完了`
183
+
184
+
185
+ `$ sudo apt-get update`←sudo実行(パスワードを聞かれない)
186
+ 〜省略(処理中)〜
187
+ `パッケージリストを読み込んでいます... 完了`
188
+
189
+
190
+ `$ apt-get update`←sudo実行してない
191
+ 〜省略(処理中)〜
192
+ `パッケージリストを読み込んでいます... 完了`
193
+
194
+ `$ exit`←端末終了
195
+ 〜端末起動〜
196
+ `$ sudo apt-get update`←sudo実行
197
+ `[sudo] password for (ユーザ名):`←パスワードを聞かれる
198
+ 〜省略(処理中)〜
199
+ `パッケージリストを読み込んでいます... 完了`
200
+
201
+ というsudoが本来なら必要(?)な処理をする際にsudoをつけなくても処理が始まったり、
202
+ sudo実行後にはパスワードを聞かれるはず(?)なのにパスワードを聞かれずに処理が始まる
203
+
204
+ という事態が発生しています。
205
+ 原因はなんでしょうか…。(これが普通なのか?)
206
+ 少し気になります。
207
+ たくさん書いてしまいすみません。
87
208
  何卒よろしくお願いします。