crontab shutdown命令永远不会运行

时间:2018-08-02 13:39:13

标签: linux ubuntu cron linuxmint

据我所知,我正在做我被告知的一切。

命令行中的命令“ shutdown -P now”完全符合我的要求-立即关闭计算机。但是,使用该命令作为cron作业绝不会发生。

我只希望它在星期一至星期五的晚上8:30关闭,然后在9:30 pm和10:30 pm关闭。所以这就是我的工作;

我键入“ crontab -e”,并将其添加到文件的底部:

30 20,21,22 * * 1-5 shutdown -P now

我按CTRL + O来“保存”它,然后按CTRL + X退出。我在提示符下收到“ crontab:安装新的crontab”消息。

但是晚上8:30到来,没有关机。也不在9:30p或10:30 pm。


编辑:浏览此处提供的列表; CronJob not running

我在“测试cron正在工作”时停止了。我输入命令

* * * * * /bin/echo "cron works" >> /tmp/file

我得到了错误

Apps: command not found

在Google中搜索此错误不会显示任何相关信息。

此外,我终于能够看到(由于链接)从哪里获取消息日志输出-我在Linux Mint中(应该提到),所以我需要Ubuntu的方式来找到日志输出。关闭命令似乎正在运行,并且唯一的“错误”消息是“未安装MTA”,(从我刚刚阅读的内容来看)该消息仅用于通过电子邮件发送输出。否则,似乎没有任何明显的错误消息...有人可以对此有所了解吗?

domarius@Domarius-LinuxMint ~ $ grep CRON /var/log/syslog
Aug  3 08:17:01 Domarius-LinuxMint CRON[3259]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Aug  3 09:17:01 Domarius-LinuxMint CRON[3876]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Aug  3 10:17:01 Domarius-LinuxMint CRON[4122]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Aug  3 11:17:01 Domarius-LinuxMint CRON[4349]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Aug  3 12:17:01 Domarius-LinuxMint CRON[4719]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Aug  3 13:17:01 Domarius-LinuxMint CRON[5028]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Aug  3 14:17:01 Domarius-LinuxMint CRON[5317]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Aug  3 15:17:01 Domarius-LinuxMint CRON[5933]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Aug  3 16:17:01 Domarius-LinuxMint CRON[7083]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Aug  3 20:26:38 Domarius-LinuxMint cron[1089]: (CRON) INFO (pidfile fd = 3)
Aug  3 20:26:38 Domarius-LinuxMint cron[1089]: (CRON) INFO (Running @reboot jobs)
Aug  3 20:30:01 Domarius-LinuxMint CRON[2670]: (root) CMD (shutdown -P now "Shutting down...")
Aug  3 20:30:01 Domarius-LinuxMint CRON[2671]: (domarius) CMD (/sbin/shutdown -p now)
Aug  3 20:30:01 Domarius-LinuxMint CRON[2669]: (CRON) info (No MTA installed, discarding output)
Aug  3 20:30:01 Domarius-LinuxMint CRON[2668]: (CRON) info (No MTA installed, discarding output)
Aug  3 21:17:01 Domarius-LinuxMint CRON[3058]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Aug  3 21:30:01 Domarius-LinuxMint CRON[3232]: (domarius) CMD (/sbin/shutdown -p now)
Aug  3 21:30:01 Domarius-LinuxMint CRON[3233]: (root) CMD (shutdown -P now "Shutting down...")
Aug  3 21:30:01 Domarius-LinuxMint CRON[3231]: (CRON) info (No MTA installed, discarding output)
Aug  3 21:30:01 Domarius-LinuxMint CRON[3230]: (CRON) info (No MTA installed, discarding output)
Aug  3 22:17:01 Domarius-LinuxMint CRON[3515]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)

4 个答案:

答案 0 :(得分:0)

通常在public class MessagingService extends FirebaseMessagingService{ private static final String TAG = "MyFirebaseMsgService"; private PreferenceManager preferenceManager = null; private static int MY_NOTIFICATION_ID = 101; Bitmap bitmap; private static int count = 0; NotificationManager notificationManager = null; @Override public void onMessageReceived(RemoteMessage remoteMessage) { // ... preferenceManager = PreferenceManager.getInstance(this); notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); // Check if message contains a data payload. if (remoteMessage.getData().size() > 0) { String title = remoteMessage.getData().get("title"); String body = remoteMessage.getData().get("body"); String message = remoteMessage.getData().get("message"); String clickAction = remoteMessage.getData().get("click_action"); PendingIntent pendingIntent = null; if(clickAction.equals("new request")){ Intent myIntent = new Intent(this, HomeActivity.class); myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK); myIntent.putExtra("title",title); myIntent.putExtra("message",message); myIntent.putExtra("body",body); pendingIntent = PendingIntent.getActivity( this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); }else if(clickAction.equals("new action")){ Intent myIntent = new Intent(this, ActionActivity.class); myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK); //fetching data from push notification String amount = remoteMessage.getData().get("amount"); String status = remoteMessage.getData().get("status"); String persons = remoteMessage.getData().get("persons"); String user_id = remoteMessage.getData().get("user_id"); String id = remoteMessage.getData().get("id"); pendingIntent = PendingIntent.getActivity( this, 0, myIntent, PendingIntent.FLAG_ONE_SHOT); //check whether app is running or not if (Helper.isAppRunning(this, "com.imtiaz.testapp")) { //notificationManager.cancelAll(); Intent intentNotification = new Intent(); intentNotification.setAction("com.imtiaz.testapp.messagingService"); intentNotification.putExtra("checkedin",checkedInItemModel); intentNotification.putExtra("type","check_in"); sendBroadcast(intentNotification); } else { // App is not running } }else if(clickAction.equals("checkedout")){ Intent myIntent = new Intent(this, HomeActivity.class); myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK); myIntent.putExtra("title",body); myIntent.putExtra("message",message); myIntent.putExtra("body",title); pendingIntent = PendingIntent.getActivity( this, 0, myIntent, PendingIntent.FLAG_ONE_SHOT); //save checked in model data preferenceManager.setIsBookingEnabled(false); preferenceManager.setCheckedInData(null); //check whether app is running or not if (Helper.isAppRunning(this, "com.imtiaz.testapp")) { notificationManager.cancelAll(); Intent intentNotification = new Intent(); intentNotification.setAction("com.imtiaz.testapp.messagingService"); intentNotification.putExtra("type","check_out"); intentNotification.putExtra("body",body); intentNotification.putExtra("message",message); intentNotification.putExtra("title",title); sendBroadcast(intentNotification); } else { // App is not running } } Notification myNotification = new NotificationCompat.Builder(this) .setContentTitle(title) .setContentText(body) .setTicker("Notification!") .setWhen(System.currentTimeMillis()) .setContentIntent(pendingIntent) .setDefaults(Notification.DEFAULT_SOUND) .setAutoCancel(true) .setSmallIcon(R.drawable.testapp) .build(); MY_NOTIFICATION_ID ++; notificationManager.notify(MY_NOTIFICATION_ID, myNotification); } if (remoteMessage.getNotification() != null) { Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); } } private Bitmap getCircleBitmap(Bitmap bitmap) { final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(output); final int color = Color.RED; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawOval(rectF, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); bitmap.recycle(); return output; }} 中检查您的cron日志。很有可能是路径问题,请使用此功能。

/var/log/cron

答案 1 :(得分:0)

在我的问题被无意中打败,并提供了许多无法正常工作的链接后,我在Linux Mint论坛上获得了我所需要的帮助-秘密是使用sudo命令编辑crontab; “ sudo crontab -e”和其他有用的信息在这篇文章中。 https://forums.linuxmint.com/viewtopic.php?f=47&t=275029&p=1507982#p1507982

答案 2 :(得分:0)

sudo vim / etc / crontab

每天10:00 PM关机的桌面

00 22 * * *根/ sbin / poweroff

答案 3 :(得分:0)

04/13/2021 更新:

运行:Ubuntu 20

我用来做 cronjob 的步骤在下午 3 点 45 分关闭计算机

  1. $ sudo crontab -e
  2. 45 13 * * 1-5 sudo shutdown -h now